Skip to content

Commit f83f926

Browse files
authored
Merge pull request #348 from sun912/main
[sun]WEEK 2 Solution
2 parents dfd73f6 + 4bff29b commit f83f926

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
class Solution:
12+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
13+
if inorder:
14+
idx = inorder.index(preorder.pop(0))
15+
node = TreeNode(inorder[idx])
16+
node.left = self.buildTree(preorder, inorder[0:idx])
17+
node.right = self.buildTree(preorder, inorder[idx+1:])
18+
19+
return node

counting-bits/sun912.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
6+
7+
class Solution:
8+
def countBits(self, n: int) -> List[int]:
9+
dp = [0] * (n+1)
10+
offset = 1
11+
12+
for i in range(1, n+1):
13+
if offset * 2 == i:
14+
offset = i
15+
dp[i] = 1 + dp[i-offset]
16+
return dp
17+
18+

decode-ways/sun912.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
5+
DNF, read solutions by Dale...ㅠㅠ
6+
"""
7+
8+
class Solution:
9+
def numDecodings(self, s: str) -> int:
10+
memo = {len(s): 1}
11+
12+
def dfs(start):
13+
if start in memo:
14+
return memo[start]
15+
16+
if s[start] == "0":
17+
return 0
18+
if start + 1 < len(s) and int(s[start: start+2]) < 27:
19+
memo[start] = dfs(start + 1) + dfs(start + 2)
20+
else:
21+
memo[start] = dfs(start + 1)
22+
return memo[start]
23+
24+
return dfs(0)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
TC: O(n)
3+
4+
"""
5+
class Solution:
6+
"""
7+
@param: strs: a list of strings
8+
@return: encodes a list of strings to a single string.
9+
"""
10+
def encode(self, strs):
11+
result = ""
12+
for str in strs:
13+
result += str(len(str)) + "#" + str
14+
return result
15+
16+
"""
17+
@param: str: A string
18+
@return: decodes a single string to a list of strings
19+
"""
20+
def decode(self, str):
21+
result = []
22+
idx = 0
23+
while idx < len(str):
24+
temp_idx = idx
25+
while str[temp_idx] != "#":
26+
temp_idx += 1
27+
length = int(str[idx:temp_idx])
28+
result.append(str[temp_idx+1:temp_idx+length+1])
29+
idx = temp_idx + length + 1
30+
return result

valid-anagram/sun912.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
6+
count_s = {}
7+
count_t = {}
8+
9+
for i in range(len(s)):
10+
count_s[s[i]] = 1 + count_s.get(s[i], 0)
11+
count_t[t[i]] = 1 + count_t.get(t[i], 0)
12+
13+
for c in count_t:
14+
if count_t[c] != count_s.get(c, 0):
15+
return False
16+
return True

0 commit comments

Comments
 (0)