-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.括号生成.py
More file actions
48 lines (46 loc) · 1013 Bytes
/
22.括号生成.py
File metadata and controls
48 lines (46 loc) · 1013 Bytes
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
39
40
41
42
43
44
45
46
#
# @lc app=leetcode.cn id=22 lang=python
#
# [22] 括号生成
#
# https://leetcode-cn.com/problems/generate-parentheses/description/
#
# algorithms
# Medium (71.78%)
# Likes: 518
# Dislikes: 0
# Total Accepted: 40.6K
# Total Submissions: 56.5K
# Testcase Example: '3'
#
# 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
#
# 例如,给出 n = 3,生成结果为:
#
# [
# "((()))",
# "(()())",
# "(())()",
# "()(())",
# "()()()"
# ]
#
#
#
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
ans=[]
def backward(s='',left=0,right=0):
if len(s)==2*n:
ans.append(s)
return
if left<n:
backward(s+'(',left+1,right)
if right<left:
backward(s+')',left,right+1)
backward()
return ans