Skip to content

Commit 8ab45ef

Browse files
Add files via upload
1 parent f9cdfe3 commit 8ab45ef

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 40ms 90.62%
2+
class Solution:
3+
def generateParenthesis(self, n):
4+
"""
5+
:type n: int
6+
:rtype: List[str]
7+
"""
8+
res_list = []
9+
def gen(strs, num_open, num_close):
10+
"""
11+
num_open: 左括号的数量
12+
num_close: 右括号的数量
13+
"""
14+
if len(strs) == 2 * n:
15+
res_list.append(strs)
16+
if num_open < n:
17+
gen(strs + '(', num_open + 1, num_close)
18+
if num_open > num_close:
19+
gen(strs + ')', num_open, num_close + 1)
20+
21+
gen('', 0, 0)
22+
return res_list

0 commit comments

Comments
 (0)