-
-
Notifications
You must be signed in to change notification settings - Fork 247
[hoyeongkwak] Week2 Solutions #1772
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,32 @@ | ||
/* | ||
Time Complexity : O(n^2) | ||
Space Complexity : O(1) | ||
*/ | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length - 2; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
int low = i + 1; | ||
int high = nums.length - 1; | ||
while (low < high) { | ||
int threeSum = nums[i] + nums[low] + nums[high]; | ||
if (threeSum < 0) { | ||
low = low + 1; | ||
} else if (threeSum > 0) { | ||
high = high - 1; | ||
} else { | ||
result.add(Arrays.asList(nums[i], nums[low], nums[high])); | ||
while (low < high && nums[low] == nums[low + 1]) low++; | ||
while (low < high && nums[high] == nums[high - 1]) high--; | ||
low = low + 1; | ||
high = high - 1; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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 @@ | ||
/* | ||
Time Complexity : O(n) | ||
Space Complexity : O(1) | ||
*/ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
if (n < 3) return n; | ||
int prev = 1; | ||
int curr = 2; | ||
|
||
for(int i = 0; i < n - 2; i++) { | ||
int temp = prev; | ||
prev = curr; | ||
curr = temp + curr; | ||
} | ||
return curr; | ||
} | ||
} |
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,23 @@ | ||
/* | ||
Time Complexity : O(n) | ||
Space Complexity : O(n) | ||
*/ | ||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
int[] results = new int[nums.length]; | ||
Arrays.fill(results, 1); | ||
int before = 1; | ||
int after = 1; | ||
|
||
for (int i = 0; i < nums.length - 1; i++) { | ||
before = before * nums[i]; | ||
results[i + 1] = results[i + 1] * before; | ||
} | ||
|
||
for (int i = nums.length - 1; i > 0; i--) { | ||
after = after * nums[i]; | ||
results[i - 1] = results[i - 1] * after; | ||
} | ||
return results; | ||
} | ||
} |
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,28 @@ | ||
/* | ||
- 애너그램 또는 어구정철 : 문장의 순서를 바꾸어 다른 단어로 만드는 것 | ||
- 같은 단어가 맞는지 검증 | ||
- s와 t의 각 알파벳별로 한쪽은 증가, 한쪽은 감소하면서 갯수 세기 | ||
- 갯수가 0이 아닌 경우엔 서로 다른 단어이기에 false | ||
*/ | ||
/* | ||
Time Complexity : O(n) | ||
Space Complexity : O(1) | ||
*/ | ||
class Solution { | ||
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,29 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
/* | ||
Time Complexity : O(n) | ||
Space Complexity : O(n) | ||
*/ | ||
class Solution { | ||
TreeNode prev; | ||
public boolean isValidBST(TreeNode root) { | ||
if (root == null) return true; | ||
if (!isValidBST(root.left)) return false; | ||
if (prev != null && prev.val >= root.val) return false; | ||
prev = root; | ||
return isValidBST(root.right); | ||
} | ||
} |
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.
배열 사용없이 공간효율성까지 챙긴 부분이 좋습니다!