Skip to content

Commit 9940e00

Browse files
committed
20_Valid_Parentheses
133_Clone_Graph 150_Evaluate_Reverse_Polish_Notation 155_Min_Stack
1 parent d1198d1 commit 9940e00

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/15_3Sum.py) | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) |
1919
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/16_3Sum_Closest.py) | Sort and Multiple Two Sum check abs|
2020
| 18 | [4Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/18_4Sum.py) | The same as 3Sum, but we can merge pairs with the same sum |
21+
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/20_Valid_Parentheses.py) | 1. Stack<br>2. Replace all parentheses with '', if empty then True |
2122
| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/21_Merge_Two_Sorted_Lists.py) | Add a dummy head, then merge two sorted list in O(m+n) |
2223
| 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/23_Merge_k_Sorted_Lists.py) | 1. Priority queue O(nk log k)<br>2. Binary merge O(nk log k)| |
2324
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/24_Swap_Nodes_in_Pairs.py) | Add a dummy and store the prev |
@@ -34,10 +35,13 @@
3435
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/111_Minimum_Depth_of_Binary_Tree.py) | 1. Recursion, note that when size of left (ld) or right (rd) is 0, then min = 1 + ld + rd<br> 2. BFS check by level (right most), which is much faster than recursion |
3536
| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/124. Binary Tree Maximum Path Sum.py) | Recursion O(n) and O(n), max (left + node, right + node, left + node + right) |
3637
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/125_Valid_Palindrome.py)| Exclude non-alphanumeric characters and compare O(n) |
38+
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/133_Clone_Graph.py) | Hash and DFS or BFS |
3739
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/136_Single_Number.py) | 1. Hash or set, O(n) and O(n)<br>2. xor O(n) and O(1) |
3840
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/137_Single_Number_II.py) | 1. ctypes 32 % 3 and &, O(n) and O(1)<br>2. ones, twos, threes as bitmask (e.g. ones represents ith bit had appeared once), O(n) and O(1) |
39-
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/138_Copy_List_with_Random_Pointer.py) | 1. Hash O(n) and O(n)<br>2. Modify original structure: Original->Copy->Original, then node.next.random = node.random.next, O(n) and O(1)|
41+
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/138_Copy_List_with_Random_Pointer.py) | 1. Hash O(n) and O(n)<br>2. Modify original structure: Original->Copy->Original, then node.next.random = node.random.next, O(n) and O(1) |
42+
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/150_Evaluate_Reverse_Polish_Notation.py) | Stack |
4043
| 151 | [Reverse Words in a String](https://discuss.leetcode.com/category/159/reverse-words-in-a-string) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/151_Reverse_Words_in_a_String.py)| 1. Python split by space <br>2. Reverse all and reverse words |
44+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/155_Min_Stack.py) | Add another stack for min stack, maintance this stack when the main stack pop or push |
4145
| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/156_Binary_Tree_Upside_Down.py) | p.left = parent.right, parent.right = p.right, p.right = parent, parent = p.left, p = left |
4246
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/157_Read_N_Characters_Given_Read4.py) | Handle the edge case (the end) |
4347
| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/158_Read_N_Characters_Given_Read4_II_Call_multiple_times.py) | Store the pos and offset that is read by last read4 |

python/133_Clone_Graph.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,43 @@
66

77
class Solution(object):
88

9-
def __init__(self):
10-
self.label_map = {}
9+
# def __init__(self):
10+
# self.label_map = {}
11+
12+
# def cloneGraph(self, node):
13+
# """
14+
# :type node: UndirectedGraphNode
15+
# :rtype: UndirectedGraphNode
16+
# """
17+
# # DFS
18+
# if node is None:
19+
# return None
20+
# res = UndirectedGraphNode(node.label)
21+
# self.label_map[node.label] = res
22+
# for ne in node.neighbors:
23+
# if ne.label not in self.label_map:
24+
# res.neighbors.append(self.cloneGraph(ne))
25+
# else:
26+
# res.neighbors.append(self.label_map[ne.label])
27+
# return res
1128

1229
def cloneGraph(self, node):
13-
"""
14-
:type node: UndirectedGraphNode
15-
:rtype: UndirectedGraphNode
16-
"""
30+
# BFS
1731
if node is None:
1832
return None
19-
res = UndirectedGraphNode(node.label)
20-
self.label_map[node.label] = res
21-
for ne in node.neighbors:
22-
if ne.label not in self.label_map:
23-
res.neighbors.append(self.cloneGraph(ne))
24-
else:
25-
res.neighbors.append(self.label_map[ne.label])
26-
return res
33+
label_map = {}
34+
queue = [node]
35+
graphCopy = UndirectedGraphNode(node.label)
36+
label_map[node.label] = graphCopy
37+
while len(queue) > 0:
38+
curr = queue.pop(0)
39+
for ne in curr.neighbors:
40+
if ne.label in label_map:
41+
label_map[curr.label].neighbors.append(label_map[ne.label])
42+
else:
43+
neighborCopy = UndirectedGraphNode(ne.label)
44+
label_map[curr.label].neighbors.append(neighborCopy)
45+
label_map[ne.label] = neighborCopy
46+
queue.append(ne)
47+
return graphCopy
48+

python/20_Valid_Parentheses.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# class Solution(object):
22
# def isValid(self, s):
3-
# """
4-
# :type s: str
5-
# :rtype: bool
6-
# """
3+
74
#
85
class Solution:
9-
# @return a boolean
106
def isValid(self, s):
7+
"""
8+
:type s: str
9+
:rtype: bool
10+
"""
1111
if s is None:
1212
return True
1313
stack = []

0 commit comments

Comments
 (0)