-
-
Notifications
You must be signed in to change notification settings - Fork 304
[kimjunyoung90] WEEK 02 solutions #2057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+109
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a4b29c7
valid-anagram solution
kimjunyoung90 7d7ef4d
Merge branch 'DaleStudy:main' into main
kimjunyoung90 647de97
climbing-stairs solution
kimjunyoung90 5fe4ef9
Merge branch 'DaleStudy:main' into main
kimjunyoung90 7aa7393
product-of-array-except-self solution
kimjunyoung90 40afe41
Merge branch 'DaleStudy:main' into main
kimjunyoung90 46ec388
3sum solution
kimjunyoung90 2e7b005
Validate Binary Search Tree solution
kimjunyoung90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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,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; | ||
| } | ||
| return b; | ||
| } | ||
| } | ||
This file contains hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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,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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열 안쓰고, 세 개의 변수를 이용하여, 최댓값을 구하는 것이 인상적이었습니다.