forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-parentheses.py
57 lines (53 loc) · 1.7 KB
/
generate-parentheses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Time: O(4^n / n^(3/2)) ~= Catalan numbers
# Space: O(n)
# iterative solution
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
result, curr = [], []
stk = [(1, (n, n))]
while stk:
step, args = stk.pop()
if step == 1:
left, right = args
if left == 0 and right == 0:
result.append("".join(curr))
if left < right:
stk.append((3, tuple()))
stk.append((1, (left, right-1)))
stk.append((2, (')')))
if left > 0:
stk.append((3, tuple()))
stk.append((1, (left-1, right)))
stk.append((2, ('(')))
elif step == 2:
curr.append(args[0])
elif step == 3:
curr.pop()
return result
# Time: O(4^n / n^(3/2)) ~= Catalan numbers
# Space: O(n)
# recursive solution
class Solution2(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def generateParenthesisRecu(left, right, curr, result):
if left == 0 and right == 0:
result.append("".join(curr))
if left > 0:
curr.append('(')
generateParenthesisRecu(left-1, right, curr, result)
curr.pop()
if left < right:
curr.append(')')
generateParenthesisRecu(left, right-1, curr, result)
curr.pop()
result = []
generateParenthesisRecu(n, n, [], result)
return result