- 括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
例如,给出 n = 3,生成结果为:
[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]
画图以后,可以分析出的结论:
当前左右括号都有大于 00 个可以使用的时候,才产生分支;
产生左分支的时候,只看当前是否还有左括号可以使用;
产生右分支的时候,还受到左分支的限制,右边剩余可以使用的括号数量一定得在严格大于左边剩余的数量的时候,才可以产生分支;
在左边和右边剩余的括号数都等于 00 的时候结算。
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
self.dfs('', n, n, res)
return res
def dfs(self, temp, left, right, res):
if left == right == 0:
res.append(temp)
return
if left > right:
return
if left:
self.dfs(temp + '(', left - 1, right, res)
if right:
self.dfs(temp + ')', left, right - 1, res)