- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path22.py
36 lines (28 loc) · 695 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
'''
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
'''
classSolution(object):
defgenerateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
result= []
defbacktracking(S, left, right):
iflen(S) ==2*n:
result.append(S)
return
ifleft<n:
backtracking(S+'(', left+1, right)
ifright<left:
backtracking(S+')', left, right+1)
backtracking('', 0, 0)
returnresult