LeetCode--022--括号生成(python)

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

例如,给出 = 3,生成结果为:

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res=[]
        def helper(left,right,tmp):
            if left==n and right==n:
                res.append(tmp)
                return 
            if left < n:
                helper(left+1,right,tmp+"(")
            if right < left and right < n:
                helper(left,right+1,tmp+")")
        helper(0,0,"")
        return res

2019-12-02 09:00:49  

猜你喜欢

转载自www.cnblogs.com/NPC-assange/p/11968753.html