-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
35 lines (24 loc) · 804 Bytes
/
answer.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
#!/usr/bin/python
#------------------------------------------------------------------------------
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def generate(n, str, left, right, arr):
if len(str) == n*2:
arr.append(str)
return
if left < n:
generate(n, str+"(", left+1, right, arr)
if right < left:
generate(n, str+")", left, right+1, arr)
arr = []
generate(n, "", 0, 0, arr)
return arr
#------------------------------------------------------------------------------
#Testing
def main():
print Solution().generateParenthesis(4)
main()