-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from f-exuan21/main
[์ํ] WEEK02 Solutions
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
construct-binary-tree-from-preorder-and-inorder-traversal/f-exuan21.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,50 @@ | ||
/** | ||
* 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 : O(n) | ||
// space : O(n) | ||
// n์ ํธ๋ฆฌ ๋ ธ๋ ์ | ||
|
||
class Solution { | ||
|
||
private int i = 0; | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
|
||
for(int i = 0; i < inorder.length; i++) { | ||
map.put(inorder[i], i); | ||
} | ||
|
||
return build(preorder, inorder, 0, inorder.length); | ||
|
||
} | ||
|
||
private TreeNode build(int[] preorder, int[] inorder, int start, int end) { | ||
if(i >= preorder.length || start >= end) { | ||
return null; | ||
} | ||
|
||
int value = preorder[i++]; | ||
int index = map.get(value); | ||
|
||
TreeNode leftTreeNode = build(preorder, inorder, start, index); | ||
TreeNode rightTreeNode = build(preorder, inorder, index+1, end); | ||
|
||
return new TreeNode(value, leftTreeNode, rightTreeNode); | ||
} | ||
|
||
} |
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,18 @@ | ||
//time : O(n) | ||
//space : O(n) | ||
|
||
class Solution { | ||
|
||
// f(n) = f(n >> 1) + (n & 1) | ||
|
||
public int[] countBits(int n) { | ||
int[] answer = new int[n + 1]; | ||
|
||
answer[0] = 0; | ||
for(int i = 1; i <= n; i++) { | ||
answer[i] = answer[i >> 1] + (i & 1); | ||
} | ||
|
||
return answer; | ||
} | ||
} |
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,24 @@ | ||
// time : O(n) | ||
// space : O(1) | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
|
||
if(s.length() != t.length()) return false; | ||
|
||
int[] charCount = new int[26]; | ||
|
||
for(int i = 0; i < s.length(); i++) { | ||
charCount[s.charAt(i) - 'a']++; | ||
charCount[t.charAt(i) - 'a']--; | ||
} | ||
|
||
for(int i : charCount) { | ||
if(i != 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |