-
Notifications
You must be signed in to change notification settings - Fork 126
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
[TONY] WEEK 02 Solutions #338
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
23 changes: 23 additions & 0 deletions
23
construct-binary-tree-from-preorder-and-inorder-traversal/TomyKim9401.java
This file contains 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 { | ||
private int i, p; | ||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
return builder(preorder, inorder, Integer.MIN_VALUE); | ||
} | ||
|
||
private TreeNode builder(int[] preorder, int[] inorder, int stop) { | ||
if (p >= preorder.length) return null; | ||
if (inorder[i] == stop) { | ||
i += 1; | ||
return null; | ||
} | ||
|
||
TreeNode node = new TreeNode(preorder[p]); | ||
p += 1; | ||
|
||
node.left = builder(preorder, inorder, node.val); | ||
node.right = builder(preorder, inorder, stop); | ||
return node; | ||
} | ||
} |
This file contains 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,10 @@ | ||
class Solution { | ||
public int[] countBits(int n) { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
int[] output = new int[n+1]; | ||
int num = 0; | ||
while (num <= n) output[num] = Integer.bitCount(num++); | ||
return output; | ||
} | ||
} |
This file contains 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,21 @@ | ||
class Solution { | ||
public int numDecodings(String s) { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
if (s.charAt(0) == '0') return 0; | ||
|
||
int[] dp = new int[s.length() + 1]; | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (int i = 2; i <= s.length(); i++) { | ||
int oneDigit = Integer.parseInt(s.substring(i-1, i)); | ||
int twoDigits = Integer.parseInt(s.substring(i-2, i)); | ||
|
||
if (oneDigit > 0 && oneDigit < 10) dp[i] += dp[i-1]; | ||
if (twoDigits >= 10 && twoDigits <= 26) dp[i] += dp[i-2]; | ||
} | ||
|
||
return dp[s.length()]; | ||
} | ||
} |
This file contains 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 @@ | ||
public class Solution { | ||
/* | ||
* @param strs: a list of strings | ||
* @return: encodes a list of strings to a single string. | ||
*/ | ||
public String encode(List<String> strs) { | ||
// write your code here | ||
StringBuilder sb = new StringBuilder(); | ||
for (String str : strs) { | ||
sb.append(str.length()).append("#").append(str); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/* | ||
* @param str: A string | ||
* @return: decodes a single string to a list of strings | ||
*/ | ||
public List<String> decode(String str) { | ||
// write your code here | ||
List<String> output = new ArrayList<>(); | ||
int i = 0; | ||
while (i < str.length()) { | ||
int idx = str.indexOf('#', i); | ||
int length = Integer.parseInt(str.substring(i, idx)); | ||
String s = str.substring(idx + 1, idx + 1 + length); | ||
output.add(s); | ||
i = idx + 1 + length; | ||
} | ||
return output; | ||
} | ||
} |
This file contains 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 @@ | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
// time complexity: O(n log n) | ||
// space complexity: O(n) | ||
if (s.length() != t.length()) return false; | ||
|
||
char[] sArr = s.toCharArray(); | ||
char[] tArr = t.toCharArray(); | ||
|
||
Arrays.sort(sArr); | ||
Arrays.sort(tArr); | ||
|
||
return Arrays.equals(sArr, tArr); | ||
} | ||
} |
Oops, something went wrong.
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.
오호, 이런 게 있었군요!
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.
해당 숫자의 bit에서 1의 갯수를 구해주는 내장 함수입니다ㅎㅎ
잘 쓰시면 도움 되실꺼에요. 시간 복잡도는 O(1) 입니다!