-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-括号生成.py
39 lines (34 loc) · 941 Bytes
/
22-括号生成.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
from typing import List
import pdb
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
# 暴力生成,逐一验证
# 递归生成
def generate(A):
print("sdgfasf")
if len(A) == 2*n:
if valid(A):
ans.append("".join(A))
else:
print(A)
# pdb.set_trace()
A.append('(')
generate(A)
A.pop()
A.append(')')
generate(A)
A.pop()
def valid(A):
bal = 0
for c in A:
if c == '(': bal += 1
else: bal -= 1
if bal < 0: return False
return bal == 0
ans = []
# pdb.set_trace()
generate([])
# pdb.set_trace()
return ans
sol = Solution()
print(sol.generateParenthesis(3))