Sharing my 88% python code, a little bit tricky


  • 0
    Y
    class Solution(object):
        def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            def generate(leftnum,rightnum,s,result):
                if leftnum == 0 and rightnum == 0:
                    result.append(s)
                if leftnum > 0:
                    generate(leftnum - 1,rightnum,s+'(',result ) 
                if rightnum > 0 and leftnum < rightnum:
                    generate(leftnum, rightnum - 1,s+')',result)
            result = []
            s = ''
            generate(n,n,s,result)
            return result

Log in to reply
 

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