Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions 3sum/kimjunyoung90.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class kimjunyoung90 {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> answers = new ArrayList<>();
//숫자들을 미리 정렬해서 3번째 단계에서 추가적인 정렬을 없게 만들자..
Arrays.sort(nums);

// 1. 요소가 중복되지 않는 3개의 숫자 조합을 찾음
for (int i = 0; i < nums.length - 2; i++) {

//i 값이 전 요소랑 같은 경우 탐색 건너 띄기
if (i > 0 && nums[i] == nums[i - 1]) continue;

//two pointer 적용
int left = i + 1;
int right = nums.length - 1;

while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
answers.add(Arrays.asList(nums[i], nums[left], nums[right]));

//left 중복 체크
while (left < right && nums[left] == nums[left + 1]) left++;

//right 중복 체크
while (left < right && nums[right] == nums[right - 1]) right--;

left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}

return answers;
}
}
16 changes: 16 additions & 0 deletions climbing-stairs/kimjunyoung90.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class kimjunyoung90 {
public int climbStairs(int n) {
if(n == 1) return 1;
if(n == 2) return 2;

int a = 1;
int b = 2;

for(int i = 3; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열 안쓰고, 세 개의 변수를 이용하여, 최댓값을 구하는 것이 인상적이었습니다.

return b;
}
}
20 changes: 20 additions & 0 deletions product-of-array-except-self/kimjunyoung90.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class kimjunyoung90 {
public int[] productExceptSelf(int[] nums) {
int[] answers = new int[nums.length];

//왼쪽 값 계산
answers[0] = 1;
for (int i = 1; i < nums.length; i++) {
answers[i] = answers[i - 1] * nums[i - 1];
}

//오른쪽 값 계산
int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
answers[i] *= right;
//다음 값 right 계산
right *= nums[i];
}
return answers;
}
}
18 changes: 18 additions & 0 deletions valid-anagram/kimjunyoung90.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class kimjunyoung90 {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;

int[] count = new int[26];

for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
count[t.charAt(i) - 'a']--;
}

for(int c : count) {
if (c != 0) return false;
}

return true;
}
}
11 changes: 11 additions & 0 deletions validate-binary-search-tree/kimjunyoung90.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class kimjunyoung90 {
public boolean isValidBST(TreeNode root) {
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

public boolean validate(TreeNode node, long min, long max) {
if(node == null) return true;
if(node.val <= min || node.val >= max) return false;
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
}
}