forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (32 loc) · 998 Bytes
/
main.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
# 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:
# "((()))", "(()())", "(())()", "()(())", "()()()"
'''
Created on 2013-5-19
@author: Yubin Bai
'''
class Solution:
# @param an integer
# @return a list of string
def generateParenthesis(self, n):
def _generateParentheses(left, right):
if left + right == n * 2:
result.append(''.join(path))
if left < n:
path.append('(')
_generateParentheses(left + 1, right)
path.pop()
if left > right:
path.append(')')
_generateParentheses(left, right + 1)
path.pop()
path = []
result = []
_generateParentheses(0, 0)
return result
if __name__ == '__main__':
s = Solution()
result = s.generateParenthesis(3)
for i in result:
print(i)