LintCode:生成括号

来源:互联网 发布:可以证件照软件 编辑:程序博客网 时间:2024/05/29 06:49

LintCode:生成括号这里写链接内容

class Solution:    # @param {int} n n pairs    # @return {string[]} All combinations of well-formed parentheses    def generateParenthesis(self, n):        # Write your code here        self.ans = []        res = ''        left = 0        right = 0        self.fun(left, right, res, n)        return self.ans    def fun(self, left, right, res, n):        if left == n and right == n:            self.ans.append(res)            return        if left < n:            self.fun(left+1, right, res+'(', n)        if left > right:            self.fun(left, right+1, res+')', n)
0 0
原创粉丝点击