码迷,mamicode.com
首页 > 其他好文 > 详细

22. Generate Parentheses(ML)

时间:2018-08-02 01:57:59      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:return   nat   log   air   def   bin   div   mat   htm   

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)

标签:return   nat   log   air   def   bin   div   mat   htm   

原文地址:https://www.cnblogs.com/guxuanqing/p/9404448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!