Skip to content

Commit 46e715f

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent d96c36f commit 46e715f

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

group-anagrams.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#https://leetcode.com/problems/group-anagrams/
22
class Solution(object):
33
def groupAnagrams(self, strs):
4-
dic = collections.defaultdict(list)
5-
6-
for string in strs:
7-
string_count = [0]*26
8-
a = ord('a')
9-
for char in string:
10-
string_count[ord(char)-a] += 1
11-
12-
dic[tuple(string_count)].append(string)
13-
14-
return dic.values()
4+
anagrams = collections.defaultdict(list)
5+
for s in strs:
6+
anagrams[''.join(sorted(s))].append(s)
7+
return anagrams.values()

jump-game.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Let's reverse engineer the process.
3+
Imagine you are standing at the last index i
4+
index i-1 will need the value>=1 to go to i (step_need=1)
5+
index i-2 will need the value>=2 to go to i (step_need=2)
6+
...
7+
8+
At a certain index x, the value>=step_need
9+
This means that, from x, we can go to i no problem.
10+
Now we are standing at x and reset step_need to 0.
11+
See if we can repeat the process until we reach the first index.
12+
"""
13+
class Solution(object):
14+
def canJump(self, nums):
15+
step_need = 0
16+
for num in reversed(nums[:-1]):
17+
step_need+=1
18+
if num>=step_need:
19+
step_need = 0
20+
return step_need==0

serialize-and-deserialize-bst.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
perform BFS to traverse the whole tree
3+
after we add the entire level to the string, then we go to the next level
4+
5+
by doing this, we can always convert the serialized tree back
6+
because when you build the tree back
7+
the order doesn't matter as long as you insert each parent before child
8+
"""
9+
10+
from collections import deque
11+
class Codec:
12+
def serialize(self, root):
13+
if root==None: return ''
14+
s=''
15+
queue = deque([root])
16+
while queue:
17+
node = queue.popleft()
18+
s+=str(node.val)+','
19+
if node.left: queue.append(node.left)
20+
if node.right: queue.append(node.right)
21+
return s[:-1]
22+
23+
def deserialize(self, data):
24+
if data==None or data=='': return None
25+
data = map(int, data.split(','))
26+
root = TreeNode(data[0])
27+
for i in xrange(1, len(data)):
28+
val = data[i]
29+
node = root
30+
while True:
31+
if val==node.val:
32+
print('Input Error')
33+
elif val>node.val:
34+
if node.right==None:
35+
node.right = TreeNode(val)
36+
break
37+
else:
38+
node = node.right
39+
else:
40+
if node.left==None:
41+
node.left = TreeNode(val)
42+
break
43+
else:
44+
node = node.left
45+
return root

valid-parentheses.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,26 @@ def isValid(self, s):
1313
return False
1414

1515

16+
class Solution(object):
17+
def isValid(self, s):
18+
stack = []
19+
for c in s:
20+
if c=='(' or c=='[' or c=='{':
21+
stack.append(c)
22+
elif c==')':
23+
if stack and stack[-1]=='(':
24+
stack.pop()
25+
else:
26+
stack.append(c)
27+
elif c==']':
28+
if stack and stack[-1]=='[':
29+
stack.pop()
30+
else:
31+
stack.append(c)
32+
elif c=='}':
33+
if stack and stack[-1]=='{':
34+
stack.pop()
35+
else:
36+
stack.append(c)
37+
return len(stack)==0
38+

0 commit comments

Comments
 (0)