Skip to content

Commit a4c6890

Browse files
committed
The 2nd week solutions
1 parent bdf08b0 commit a4c6890

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
private int i, p;
3+
public TreeNode buildTree(int[] preorder, int[] inorder) {
4+
return builder(preorder, inorder, Integer.MIN_VALUE);
5+
}
6+
7+
private TreeNode builder(int[] preorder, int[] inorder, int stop) {
8+
if (p >= preorder.length) return null;
9+
if (inorder[i] == stop) {
10+
i += 1;
11+
return null;
12+
}
13+
14+
TreeNode node = new TreeNode(preorder[p]);
15+
p += 1;
16+
17+
node.left = builder(preorder, inorder, node.val);
18+
node.right = builder(preorder, inorder, stop);
19+
return node;
20+
}
21+
}

counting-bits/TonyKim9401.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int[] countBits(int n) {
3+
// time complexity: O(n)
4+
// space complexity: O(n)
5+
int[] output = new int[n+1];
6+
int num = 0;
7+
while (num <= n) output[num] = Integer.bitCount(num++);
8+
return output;
9+
}
10+
}

decode-ways/TonyKim9401.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int numDecodings(String s) {
3+
// time complexity: O(n)
4+
// space complexity: O(n)
5+
if (s.charAt(0) == '0') return 0;
6+
7+
int[] dp = new int[s.length() + 1];
8+
dp[0] = 1;
9+
dp[1] = 1;
10+
11+
for (int i = 2; i <= s.length(); i++) {
12+
int oneDigit = Integer.parseInt(s.substring(i-1, i));
13+
int twoDigits = Integer.parseInt(s.substring(i-2, i));
14+
15+
if (oneDigit > 0 && oneDigit < 10) dp[i] += dp[i-1];
16+
if (twoDigits >= 10 && twoDigits <= 26) dp[i] += dp[i-2];
17+
}
18+
19+
return dp[s.length()];
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
// time complexity: O(n)
3+
// space complexity: O(n)
4+
Map<Integer, String> encode = new HashMap<>();
5+
/*
6+
* @param strs: a list of strings
7+
* @return: encodes a list of strings to a single string.
8+
*/
9+
public String encode(List<String> strs) {
10+
// write your code here
11+
int key = 0;
12+
for (String str : strs) encode.put(key++, str);
13+
return String.valueOf(key);
14+
}
15+
16+
/*
17+
* @param str: A string
18+
* @return: decodes a single string to a list of strings
19+
*/
20+
public List<String> decode(String str) {
21+
// write your code here
22+
List<String> output = new ArrayList<>();
23+
int decode = 0;
24+
while (decode < Integer.valueOf(str)) output.add(encode.get(decode++));
25+
return output;
26+
}
27+
}

valid-anagram/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
// time complexity: O(n log n)
4+
// space complexity: O(n)
5+
if (s.length() != t.length()) return false;
6+
7+
char[] sArr = s.toCharArray();
8+
char[] tArr = t.toCharArray();
9+
10+
Arrays.sort(sArr);
11+
Arrays.sort(tArr);
12+
13+
return Arrays.equals(sArr, tArr);
14+
}
15+
}

0 commit comments

Comments
 (0)