One line Python solution


  • 0
    C
    class Solution(object):
        def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            return ['(' + litem + ')' + ritem for i in range(n) for litem in self.generateParenthesis(i) for ritem in self.generateParenthesis(n-1-i)] or ['']
    

    Format the last line a little to make it easy to read

            return ['(' + litem + ')' + ritem \
            for i in range(n) \
            for litem in self.generateParenthesis(i) \
            for ritem in self.generateParenthesis(n-1-i)] \
            or ['']
    

Log in to reply
 

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