首页 > 编程语言 > 详细

LeetCode--022--括号生成(python)

时间:2019-12-02 09:51:38      阅读:63      评论:0      收藏:0      [点我收藏+]

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 = 3,生成结果为:

技术分享图片

 

 

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res=[]
        def helper(left,right,tmp):
            if left==n and right==n:
                res.append(tmp)
                return 
            if left < n:
                helper(left+1,right,tmp+"(")
            if right < left and right < n:
                helper(left,right+1,tmp+")")
        helper(0,0,"")
        return res

2019-12-02 09:00:49  

LeetCode--022--括号生成(python)

原文:https://www.cnblogs.com/NPC-assange/p/11968753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!