-
Notifications
You must be signed in to change notification settings - Fork 0
/
22 Generate Parentheses.py
82 lines (65 loc) · 1.96 KB
/
22 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from cgitb import reset
from collections import defaultdict
from operator import ge
import time
def def_value():
return []
# 1、穷举所有可能然后删除不符合的
# 2、提前剪枝,去除不合法的
class Solution():
def __init__(self):
pass
def solve(self, n):
def generate(n, p, results=[]):
if (n == 0):
results.append(p)
return results
generate(n-1, p=p + "(")
generate(n-1, p=p + ")")
return results
def valid(s):
stack = []
for c in s:
if c == "(":
stack.append(c)
elif(len(stack)==0 or stack.pop()!="("):
return False
return len(stack)==0
results_bef = generate(2*n, "")
results = []
for result in results_bef:
if(valid(result)):
results.append(result)
return results
def solve2(self, n):
def generate(p, left, right, parens=[]):
if right == 0:
parens.append(p)
if (left):
generate(p+"(", left-1, right)
if (right > left):
generate(p+")", left, right-1)
return parens
return generate('', n, n)
def solve3(self, n):
results = []
def generate(path, n, left, right):
if len(path) == 2 * n:
if path not in results:
results.append(path)
return
if right >= left:
generate(path+"(", n, left+1, right)
elif left >= n:
generate(path+")", n, left, right+1)
else:
generate(path+"(", n, left+1, right)
generate(path+")", n, left, right+1)
generate('', n, 0, 0)
return results
time1 = time.time()
n = 3
pro = Solution()
print(pro.solve3(n))
time2 = time.time()
print(time2-time1)