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

 

posted @ 2021-06-25 17:41  泊鸽  阅读(81)  评论(0)    收藏  举报