leetcode-python-括号生成
左括号才能放右括号,参考他人答案。
class Solution: def generateParenthesis(self, n: int) -> List[str]: res = [] def dfs(tmp, left, right): if len(tmp) == 2 * n: res.append(tmp) if left: dfs(tmp + "(", left - 1, right) if right > left: dfs(tmp + ")", left, right - 1) dfs("", n, n) return res

浙公网安备 33010602011771号