22. Generate Parentheses(ML)

Posted guxuanqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了22. Generate Parentheses(ML)相关的知识,希望对你有一定的参考价值。

22. Generate Parentheses

 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

 

class Solution(object):
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        if n == 0: return [‘‘]
        ans = []
        for c in xrange(n):
            for left in self.generateParenthesis(c):
                for right in self.generateParenthesis(n-1-c):
                    ans.append(({}){}.format(left, right))
        return ans

 

xrange and format

 

以上是关于22. Generate Parentheses(ML)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 22. Generate Parentheses

Leetcode 22. Generate Parentheses

22. Generate Parentheses(ML)

leetcode22. Generate Parentheses

22. Generate Parentheses

22. Generate Parentheses(js)