Skip to content

Commit 7fcb44c

Browse files
Merge pull request #2057 from kimjunyoung90/main
[kimjunyoung90] WEEK 02 solutions
2 parents 997693f + 2e7b005 commit 7fcb44c

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

3sum/kimjunyoung90.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class kimjunyoung90 {
6+
public List<List<Integer>> threeSum(int[] nums) {
7+
List<List<Integer>> answers = new ArrayList<>();
8+
//숫자들을 미리 정렬해서 3번째 단계에서 추가적인 정렬을 없게 만들자..
9+
Arrays.sort(nums);
10+
11+
// 1. 요소가 중복되지 않는 3개의 숫자 조합을 찾음
12+
for (int i = 0; i < nums.length - 2; i++) {
13+
14+
//i 값이 전 요소랑 같은 경우 탐색 건너 띄기
15+
if (i > 0 && nums[i] == nums[i - 1]) continue;
16+
17+
//two pointer 적용
18+
int left = i + 1;
19+
int right = nums.length - 1;
20+
21+
while (left < right) {
22+
int sum = nums[i] + nums[left] + nums[right];
23+
if (sum == 0) {
24+
answers.add(Arrays.asList(nums[i], nums[left], nums[right]));
25+
26+
//left 중복 체크
27+
while (left < right && nums[left] == nums[left + 1]) left++;
28+
29+
//right 중복 체크
30+
while (left < right && nums[right] == nums[right - 1]) right--;
31+
32+
left++;
33+
right--;
34+
} else if (sum < 0) {
35+
left++;
36+
} else {
37+
right--;
38+
}
39+
}
40+
}
41+
42+
return answers;
43+
}
44+
}

climbing-stairs/kimjunyoung90.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class kimjunyoung90 {
2+
public int climbStairs(int n) {
3+
if(n == 1) return 1;
4+
if(n == 2) return 2;
5+
6+
int a = 1;
7+
int b = 2;
8+
9+
for(int i = 3; i <= n; i++) {
10+
int c = a + b;
11+
a = b;
12+
b = c;
13+
}
14+
return b;
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class kimjunyoung90 {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] answers = new int[nums.length];
4+
5+
//왼쪽 값 계산
6+
answers[0] = 1;
7+
for (int i = 1; i < nums.length; i++) {
8+
answers[i] = answers[i - 1] * nums[i - 1];
9+
}
10+
11+
//오른쪽 값 계산
12+
int right = 1;
13+
for (int i = nums.length - 1; i >= 0; i--) {
14+
answers[i] *= right;
15+
//다음 값 right 계산
16+
right *= nums[i];
17+
}
18+
return answers;
19+
}
20+
}

valid-anagram/kimjunyoung90.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class kimjunyoung90 {
2+
public boolean isAnagram(String s, String t) {
3+
if(s.length() != t.length()) return false;
4+
5+
int[] count = new int[26];
6+
7+
for (int i = 0; i < s.length(); i++) {
8+
count[s.charAt(i) - 'a']++;
9+
count[t.charAt(i) - 'a']--;
10+
}
11+
12+
for(int c : count) {
13+
if (c != 0) return false;
14+
}
15+
16+
return true;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class kimjunyoung90 {
2+
public boolean isValidBST(TreeNode root) {
3+
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
4+
}
5+
6+
public boolean validate(TreeNode node, long min, long max) {
7+
if(node == null) return true;
8+
if(node.val <= min || node.val >= max) return false;
9+
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
10+
}
11+
}

0 commit comments

Comments
 (0)