1. 程式人生 > >22. Generate Parentheses(ML)

22. Generate Parentheses(ML)

int n) targe code append cnblogs (()) The ref

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)