leetcode:遞迴:括號生成

Justinboy發表於2020-11-09

方法一:遞迴

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res = []
        def helper(s = '',left = 0,right = 0) :  
            if len(s) == 2*n :
                res.append(s)
            if left < n :
                helper(s+'(',left+1,right)
            if right < left :
                helper(s+')',left,right+1)
        helper()
        return res

相關文章