-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #729 from yeeZinu/main
[호돌이] Week2
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
* | ||
* 문제: 세 수를 더해서 0을 만들어야함. | ||
* | ||
* 주의사항: 인덱스 상관없어 세 수의 조합이 같으면 안됨. | ||
* 핵심: 배열을 오름차순으로 정렬해서 | ||
* 양 끝에 각 인덱스넣고 사이에 있는인덱스로 계속 더하면서 | ||
* 좌우 인덱스를 0에 가깝게 +- 하면된다~ | ||
* | ||
*/ | ||
var threeSum = function (nums) { | ||
// 결과를 저장할 배열 | ||
let result = []; | ||
// 주어진 수를 오름차순으로 정렬 | ||
nums.sort((a, b) => a - b); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
// nums[i] > 0보다 크다면? 반복 끝 | ||
if (nums[i] > 0) { | ||
break; | ||
} | ||
|
||
let j = i + 1; // 중간에서 바뀔 인덱스 | ||
let k = nums.length - 1; // 맨 마지막에서 부터 움직일 인덱스 | ||
|
||
while (j < k) { | ||
let sum = nums[i] + nums[j] + nums[k]; | ||
|
||
// 총합이 양수라면 k인덱스 한칸뒤로 ㄱ | ||
if (sum > 0) { | ||
k--; | ||
} | ||
// 음수라면 j진행 ㄱ | ||
else if (sum < 0) { | ||
j++; | ||
} | ||
// 0이면 result배열에 추가, j진행 | ||
else { | ||
result.push([nums[i], nums[j], nums[k]]); | ||
j++; | ||
|
||
// j가 이전값과 같다면 무시하고 진행하기 | ||
while (nums[j] === nums[j - 1] && j < k) { | ||
j++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
// n+1 배열을 만들고 0으로 초기화 | ||
const dp = new Array(n + 1).fill(0); | ||
|
||
// 인덱스 0번과 1번은 1로 초기화 | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
// 이전계단과 그 이전 계단의 합이 계단을 올라갈 수 있는 총합 | ||
for (let i = 2; i <= n; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 2]; | ||
} | ||
|
||
return dp[n]; | ||
}; | ||
|
||
console.log(climbStairs(5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
* | ||
*/ | ||
|
||
var isAnagram = function (s, t) { | ||
// 두 문자열의 길이가 같지않다면 false | ||
if (s.length !== t.length) { | ||
return false; | ||
}; | ||
|
||
// Map을 사용해서 데이터 최신화 | ||
let count = new Map(); | ||
|
||
// set으로 해당 문자를 key, 갯수를 value로 카운트 | ||
for (let char of s) { | ||
count.set(char, (count.get(char) || 0) + 1) | ||
} | ||
|
||
for (let char of t) { | ||
// 새로운 문자가 map에 없거나, 이미 카운트가 0이라면 false | ||
if (!count.has(char) || count.get(char) === 0) { | ||
return false; | ||
} | ||
// 문자 카운트 -1 | ||
count.set(char, (count.get(char) - 1)) | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/* | ||
* 문제: 두 문자열의 구성요소 비교 | ||
* | ||
* 생각: 각 문자열에 상대 문자열의 스펠링이 있는지 확인하고 splice로 제거 | ||
* 결과: 타임리밋 | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) { | ||
return false; | ||
}; | ||
let sArr = []; | ||
let tArr = []; | ||
let finder = 0; | ||
for (let j = 0; j < s.length; j++) { | ||
sArr.push(s[j]); | ||
tArr.push(t[j]); | ||
} | ||
// 반복문을 돌며 s와 t를 비교 | ||
for (let i = 0; i < s.length; i++) { | ||
//s[i]번 째가 t안에 있다면? | ||
if (tArr.indexOf(sArr[0]) !== -1) { | ||
// 찾은 인덱스를 저장해 줌 -> sArr 에서 splice먼저해버리면 배열이 달라져서 못찾음;; | ||
finder = tArr.indexOf(sArr[0]); | ||
//s[i]를 스플라이스로 빼고 | ||
sArr.splice(0, 1); | ||
//t의 인덱스번호를 슬라이스로 뺌 | ||
tArr.splice(finder, 1); | ||
} | ||
console.log(i); | ||
console.log(sArr); | ||
console.log(tArr); | ||
} | ||
// s와 t의 길이가 0이면 true 아니면 fales | ||
if (sArr.length === 0 && tArr.length === 0) { | ||
return true; | ||
} | ||
else return false | ||
}; | ||
*/ |