Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >leetcode: 22. Generate Parentheses

leetcode: 22. Generate Parentheses

作者头像
JNingWei
发布于 2018-09-28 06:13:16
发布于 2018-09-28 06:13:16
36000
代码可运行
举报
文章被收录于专栏:JNing的专栏JNing的专栏
运行总次数:0
代码可运行

Problem

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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:
#
# [
#   "((()))",
#   "(()())",
#   "(())()",
#   "()(())",
#   "()()()"
# ]

Idea

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
l_num = 剩余的 左括号数
r_num = 剩余的 右括号数

只需要保证 l_num <= r_num ,当前的组合就一定是有效的。

AC

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution():
    def generateParenthesis(self, n):
        res = []
        self.generate(res, '', n, n)
        return res
    def generate(self, res, cur, l_num, r_num):
        if l_num <= r_num:
            if r_num == 0:
                res.append(cur)
                return
            if l_num > 0:
                self.generate(res, cur+'(', l_num-1, r_num)
            if r_num > 0:
                self.generate(res, cur+')', l_num, r_num-1)


if __name__ == "__main__":
    assert Solution().generateParenthesis(3) == ['((()))', '(()())', '(())()', '()(())', '()()()']
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年11月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode: Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
卡尔曼和玻尔兹曼谁曼
2019/01/22
3300
LeetCode 22. Generate Parentheses 生成括号 Python 回溯解法
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 给定n对括号,写一个函数来生成成对的括号的所有组合。
大鹅
2021/06/15
7980
LeetCode 22. Generate Parentheses分析代码
image.png 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。
desperate633
2018/08/22
3040
LeetCode 22. Generate Parentheses分析代码
LeetCode 0032 - Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
Reck Zhang
2021/08/11
1860
parentheses - 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
ppxai
2020/09/23
3480
【LeetCode每日一题】22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
公众号-不为谁写的歌
2020/07/23
3140
leetcode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
流川疯
2021/03/15
4640
LeetCode 22. Generate Parentheses题目分析代码
给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())", "(())()", "()(())", "()()()"
desperate633
2018/08/22
3140
LeetCode 22. Generate Parentheses题目分析代码
【Leet Code】22. Generate Parentheses
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
韩旭051
2019/11/08
3460
Leetcode 22 Generate Parentheses 搜索与DP的纠结
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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 找出所有匹配的括号字符串, 肯定是搜索啦!但是当n大的时候真的可以用搜索解决吗?
triplebee
2018/01/12
7200
LeetCode22- Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Dylan Liu
2019/07/01
3000
Leetcode 题目解析之 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
ruochen
2022/02/13
1.3K0
LeetCode之Generate Parentheses(C++)
题目: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: "((()))", "(()())", "(())()", "()(())", "()()()" 目标:生成正确的括号对 数据结构:采用二叉树结构,每个节点的value是“(”或“)”,节点结构包括左右
forrestlin
2018/05/24
4280
LeetCode 0022 - Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Reck Zhang
2021/08/11
2780
【leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
阳光岛主
2019/02/19
4870
回溯/贪心高频题
"有关递归的算法,都离不开“树”的遍历这一抽象模型。只不过对于不同的算法,在前(中)后序遍历的时候,所做的事不同而已。 "
王脸小
2019/10/28
1.4K0
LeetCode22 生成所有括号对
https://leetcode.com/problems/generate-parentheses/
TechFlow-承志
2020/03/05
4660
LeetCode-22括号生成
今天我们学习第22题括号生成,这是一道中等题。像这样字符串的题目经常作为面试题来考察面试者算法能力和写代码能力,因此最好能手写出该题。下面我们看看这道题的题目描述。
用户3470542
2019/06/26
4960
leetcode: 32. Longest Valid Parentheses [✗]
Problem # Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. # # For "(()", the longest valid parentheses substring is "()", which has length = 2. # # Another exampl
JNingWei
2018/09/28
2710
算法细节系列(15):Valid Parentheses系列
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/71599078
用户1147447
2019/05/26
4290
相关推荐
Leetcode: Generate Parentheses
更多 >
LV.2
年糕妈妈全栈开发工程师
交个朋友
加入腾讯云官网粉丝站
蹲全网底价单品 享第一手活动信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验