Share my python solution


  • 0
    _
    class Solution(object): 
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        self.l = []
        self.func("", n, n)
        return self.l   
    def func(self, s, x, y):
        if y == 0:
            self.l.append(s)
        elif x == 0:
            self.func("%s)" % s, x, y-1)
        elif x == y:
            self.func("%s(" % s, x-1, y)
        else:
            self.func("%s(" % s, x-1, y)
            self.func("%s)" % s, x, y-1)

Log in to reply
 

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