-
-
Notifications
You must be signed in to change notification settings - Fork 248
[s0ooo0k] WEEK2 solutions #1767
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
8 commits
Select commit
Hold shift + click to select a range
3cb8a47
Add valid anagram solution - s0ooo0k
s0ooo0k 5530ced
Merge branch 'main' of https://github.com/s0ooo0k/leetcode-study
s0ooo0k cc29e97
Merge branch 'DaleStudy:main' into main
s0ooo0k dda1a7d
Add climbin-stairs solution - s0ooo0k
s0ooo0k f17f3b8
Add validate binary search Solution - s0ooo0k
s0ooo0k ff34068
Add valid binary search Solution - s0ooo0k
s0ooo0k 132bd90
Add valid anagaram Solution - s0ooo0k
s0ooo0k bbabf69
Add climbing stairs Solution - s0ooo0k
s0ooo0k 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,17 @@ | ||
class Solution { | ||
/* | ||
* 시간복잡도 O(n) | ||
* 공간복잡도 O(n) | ||
*/ | ||
public int climbStairs(int n) { | ||
int[] piv = new int[n+1]; | ||
piv[0]=1; | ||
piv[1]=2; | ||
|
||
for(int i=2; i<n; i++) { | ||
piv[i]=piv[i-1]+piv[i-2]; | ||
} | ||
return piv[n-1]; | ||
} | ||
} | ||
|
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 @@ | ||
class Solution { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정렬 없이 카운팅 방식으로만 풀어보셔도 좋을 것 같아요. |
||
/* | ||
* 초반 풀이시 단순 s의 문자가 t에 포함하는지만 확인하여 제대로된 비교를 못함 (ex "sass" vs "saas") | ||
* 따라서 정렬 후 비교하는 방식으로 전환 | ||
* | ||
* 시간복잡도 O(n log n) | ||
* 공간복잡도 O(n) | ||
*/ | ||
public boolean isAnagram(String s, String t) { | ||
|
||
if(s.length() != t.length()) | ||
return false; | ||
|
||
char[] chr1 = s.toCharArray(); | ||
char[] chr2 = t.toCharArray(); | ||
|
||
Arrays.sort(chr1); | ||
Arrays.sort(chr2); | ||
|
||
return Arrays.equals(chr1, chr2); | ||
} | ||
} | ||
|
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,15 @@ | ||
/* | ||
* 시간복잡도 : O(n) | ||
*/ | ||
|
||
class Solution { | ||
public boolean isValidBST(TreeNode root) { | ||
return valid(root, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
private boolean valid(TreeNode node, long min, long max) { | ||
if(node==null) return true; | ||
if(node.val <= min || node.val >=max) return false; | ||
return valid(node.left, min, node.val) && valid(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.
예외는 이렇게 써주는 것도 명확해서 좋다고 생각합니다.
추가적으로 풀어보신다면 변수 2개만 쓰는 공간 최적화 O(1)으로도 풀 수 있습니다.
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.
해당 방식으로 쓰는게 더 명확해보이네요 :) 조언 감사합니다!