-
Notifications
You must be signed in to change notification settings - Fork 35
/
22GenerateParentheses.py
65 lines (56 loc) · 1.78 KB
/
22GenerateParentheses.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
58
59
60
61
62
63
64
65
class Solution:
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
# ans = []
# if n <= 0:
# return ans
# if n == 1:
# return ["()"]
# ans.append("()")
# k = 1
# for j in range(1, n):
# if n % j == 0:
# k = j
# for i in range(1, n + 1):
# for a in ans:
# if len(a) < 2*n:
# if a + "()" != "()" + a:
# m = int(len(a) / 2)
# ans.append(a[:m] + "()" + a[m:])
# ans.append("(" + a + ")")
# ans.append(a + "()")
# ans.append("()" + a)
# ans.remove(a)
# elif a + "()" == "()" + a:
# m = int(len(a) / 2)
# ans.append(a[:m] + "()" + a[m:])
# ans.append("(" + a + ")")
# ans.append("()" + a)
# ans.remove(a)
# # for a in ans:
# # if len(a) != 2*n:
# # ans.remove(a)
# temp = "(" * k + ")" * k
# temp = temp * k
# ans.append(temp)
# for a in ans:
# if len(a) < 2*n:
# ans.remove(a)
# ans = list(set(ans))
# return ans
ans = []
def backtrack(S='', left=0, right=0):
if len(S) == 2 * n:
ans.append(S)
return
if left < n:
backtrack(S+'(', left+1, right)
if right < left:
backtrack(S+')', left, right+1)
backtrack()
return ans
s = Solution()
print(s.generateParenthesis(4))