Python solution


  • 0
    M
    class Solution(object):      
        def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            result = []
            self.recursion(0, n, n, '', result)
            return result
            
        def recursion(self, l, n1, n2, arr, result):
            if n1 > 0: self.recursion(l + 1, n1 - 1, n2, arr + '(', result)
            if l > 0 and n2 > 0: self.recursion(l - 1, n1, n2 - 1, arr + ')', result)
            if l == 0 and n1 == 0 and n2 == 0: result.append(arr)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.