22. Generate Parentheses(ML)
阿新 • • 發佈:2018-08-02
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): defgenerateParenthesis(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)