Skip to content

Commit 9ffacb8

Browse files
committed
22 Generate Parentheses.py
1 parent 1bdb85c commit 9ffacb8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

22 Generate Parentheses.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# class Solution(object):
2+
# def generateParenthesis(self, n):
3+
# """
4+
# :type n: int
5+
# :rtype: List[str]
6+
# """
7+
# pa_list = ["("]
8+
# count = 1
9+
# while count < n*2:
10+
# new_list = []
11+
# for pa in pa_list:
12+
# left_count = pa.count("(")
13+
# right_count = pa.count(")")
14+
# if right_count < left_count:
15+
# new_list.append(pa + ")")
16+
# if left_count < n:
17+
# new_list.append(pa + "(")
18+
# pa_list = new_list
19+
# count += 1
20+
21+
# return pa_list
22+
23+
class Solution(object):
24+
def generateParenthesis(self, n):
25+
"""
26+
:type n: int
27+
:rtype: List[str]
28+
"""
29+
pa_list = []
30+
def genereat_pa(cur, left_count, right_count):
31+
if len(cur) == n*2:
32+
pa_list.append(cur)
33+
34+
if left_count < n:
35+
genereat_pa(cur + "(", left_count + 1, right_count)
36+
37+
if right_count < left_count:
38+
genereat_pa(cur + ")", left_count, right_count + 1)
39+
40+
genereat_pa("", 0, 0)
41+
return pa_list
42+
43+
obj = Solution()
44+
print(obj.generateParenthesis(1))
45+
print(obj.generateParenthesis(3))
46+
print(obj.generateParenthesis(7))

0 commit comments

Comments
 (0)