leetcode-22 括号生成

leetcode-22 括号生成

题目描述:

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

参考:负雪明烛

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        def dfs(left,right,path):
            if left == 0 and right == 0:
                res.append(path)
                return
            if left > 0:
                dfs(left-1,right,path+"(")
            if left < right:
                dfs(left,right-1,path+")")
        res = []
        dfs(n,n,"")
        return res
原文地址:https://www.cnblogs.com/curtisxiao/p/11285784.html