题目描述
方法
从“”开始生成括号,保持左括号剩余数量小于右括号
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
record = []
def recur(tmp, l, r):
if r == 0:
res.append(tmp)
return
if l>0 and l==r:
recur(tmp+"(",l-1,r)
elif l>0 and l < r:
recur(tmp+"(",l-1,r)
recur(tmp+")",l,r-1)
elif l == 0:
recur(tmp+")",l,r-1)
recur("", n, n)
return res