forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf08b0
commit a4c6890
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
class Solution { | ||
private int i, p; | ||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
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,27 @@ | ||
public class Solution { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
Map<Integer, String> encode = new HashMap<>(); | ||
/* | ||
* @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 | ||
int key = 0; | ||
for (String str : strs) encode.put(key++, str); | ||
return String.valueOf(key); | ||
} | ||
|
||
/* | ||
* @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 decode = 0; | ||
while (decode < Integer.valueOf(str)) output.add(encode.get(decode++)); | ||
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); | ||
} | ||
} |