Skip to content

Commit aa93b01

Browse files
authored
Merge pull request #2433 from Yu-Won/main
[Yu-Won] Week 2 solutions
2 parents e54bbb5 + 232899d commit aa93b01

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

3sum/Yu-Won.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/3sum/description/
3+
*
4+
* 요구사항:
5+
* nums: number[]가 주어질 때 3개의 합이 0이되는 배열을 리턴한다.
6+
*
7+
* * */
8+
9+
const threeSum = (nums) => {
10+
let result = [];
11+
12+
nums.sort((a,b) => a-b);
13+
14+
for(let i = 0; i <nums.length-2; i++) {
15+
if(i > 0 && nums[i] === nums[i-1]) continue;
16+
17+
if(nums[i] > 0) break;
18+
19+
let left= i+1;
20+
let right = nums.length -1;
21+
22+
while(left < right) {
23+
let sum = nums[i] + nums[left] + nums[right];
24+
25+
if(sum === 0) {
26+
result.push([nums[i], nums[left], nums[right]]);
27+
28+
while(left < right && nums[left] === nums[left+1]) left++;
29+
while(left < right && nums[right] === nums[right-1]) right++;
30+
left++;
31+
right--;
32+
} else if (sum <0) {
33+
left++;
34+
} else {
35+
right--;
36+
}
37+
}
38+
}
39+
return result;
40+
}

climbing-stairs/Yu-Won.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/climbing-stairs/description/
3+
*
4+
* 요구사항:
5+
* n개의 계단이 있을 때
6+
* 계단은 1, 2 칸씩 오를 수 있다.
7+
* 이 계단을 오르는 방법은 총 몇가지 인가?
8+
*
9+
* * */
10+
11+
const climbingStairs = (n) => {
12+
if(n <= 1) return 1;
13+
let prev1 = 1;
14+
let prev2 = 1;
15+
for(let i = 1; i < n; i++) {
16+
let current = prev1 + prev2;
17+
prev2 = prev1;
18+
prev1 = current;
19+
}
20+
21+
return prev1;
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/product-of-array-except-self/description/
3+
*
4+
* 요구사항:
5+
* nums: number[]가 주어질 때 자기자신을 제외한 값들의 곱을 O(n)으로 나눗셈 없이 number[]로 리턴
6+
*
7+
* * */
8+
9+
const productOfArrayExceptSelf = (nums) => {
10+
let answer = new Array(nums.length);
11+
// 가장 왼쪽에는 아무것도 없으니 1로 시작
12+
answer[0] = 1;
13+
14+
// 왼쪽에서 오른쪽으로 가면서 누적 곱
15+
for(let i = 1; i < nums.length; i++) {
16+
answer[i] = answer[i - 1] * nums[i - 1];
17+
}
18+
19+
// 가장 오른쪽 끝이 없으니 1로 시작
20+
let right = 1;
21+
22+
// 역순회
23+
for(let i = nums.length -1; i >=0; i--) {
24+
answer[i] = answer[i] * right;
25+
26+
right = right * nums[i];
27+
}
28+
29+
return answer;
30+
}

valid-anagram/Yu-Won.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/valid-anagram/description/
3+
*
4+
* 요구사항:
5+
* 두개의 문자열이 주어졌을 때 두개의 문자열이 애너그램인지 판단해서 boolean 으로 리턴
6+
*
7+
* * */
8+
9+
const validAnagram = (s, t) => {
10+
if(s.length !== t.length) return false;
11+
12+
let count = {};
13+
14+
for(const sChar of s) {
15+
count[sChar] = (count[sChar] || 0) + 1;
16+
}
17+
18+
for(const tChar of t) {
19+
if(!count[tChar]) return false;
20+
count[tChar]--;
21+
}
22+
23+
return true;
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/validate-binary-search-tree/description/
3+
*
4+
* 요구사항:
5+
* root 이진 트리 구조가 주어졌을 때, 해당 트리가 유효한 BST인지 판별하라.
6+
*
7+
* * */
8+
9+
const isValidBST = (nums) => {
10+
const validate = (node, min, max) => {
11+
if (!node) return true;
12+
13+
if (node.val <= min || node.val >= max) return false;
14+
15+
return validate(node.left, min, node.val) &&
16+
validate(node.right, node.val, max);
17+
}
18+
19+
return validate(root, -Infinity, Infinity);
20+
}

0 commit comments

Comments
 (0)