*leetcode-22-括号生成-medium

LeetCode22 生成所有括号对 本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Generate Parentheses 难度 Medium 描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 给定n对括号,要求返回所有这些括号组成的不同的合... 阅读详情

题目描述

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

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

[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/generate-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

优秀题解


法一

  • DFS,回溯
  • 重点是维护nl(左括号可以使用的数量)和nr(右括号可以使用的数量)
  • 代码比较巧妙

都可以使用时,先使用左括号
随着左括号的使用,右括号可以使用的数量才逐渐增加

  • 函数地地址引用,第一次见,在这种回溯算法中比较好用
vector<string> generateParenthesis(int n) {
    if(n==0){
        return {};
    }
    vector<string> res;
    dfs(res,"",n,0);
    return res;
}
void dfs(vector<string>& res,string s, int nl, int nr){
    if(nl==0&&nr==0){
        res.push_back(s);
    }else{
        if(nl>0){
            dfs(res,s+"(",nl-1,nr+1);
        }
        if(nr>0){
            dfs(res,s+")",nl,nr-1);
        }
    }
}

法二

  • 动态规划
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<vector<string>> dp;
        vector<string> dp0;
        dp0.push_back("");
        if(n==0) {
            //vector<string> dp0;
           // dp0.push_back("");
            return dp0;
        }

        
        dp.push_back(dp0);
        int i,j;
        for(i=1;i<=n;i++){
           // printf("i=%d\n",i);
            vector<string> dpn;
            for(j=0;j<i;j++){
                // printf("j=%d\n",j);
                vector<string> str1 = dp[j];
                vector<string> str2 = dp[i-1-j];
                for (string s1 : str2) {
                    //printf("a\n");
                    for (string s2 : str1) {
                       // printf("b\n");
                        dpn.push_back("("+s1+")"+s2);
                    }
                }
            }
            dp.push_back(dpn);
        }

        return dp[n];
    }
};

通达信强龙趋势幅图指标 - 精准捕捉趋势,把握强势行情,多角度分析个股,强弱一目了然 STICKLINE(趋势>0 AND SJTJ,4.8,5.8,1.5,0),COLORYELLOW;STICKLINE(资金>0 AND SJTJ,1.2,2.2,1.5,0),COLOR0080FF;STICKLINE(异动>0 AND SJTJ,0,1,1.5,0),COLORFF0080;STICKLINE(异动=0 AND SJTJ,0,1,1.5,1),COLORGRAY; 阅读详情

相关推荐

集成电路设计学习笔记(二)基于IC617反相器设计之原理图绘制

博主的博客网站 欢迎大家访问! ccbirds.cn ccbirds.github.io 本文主要记录在IC617中原理图的绘制和schematic的使用方法。 新建library 要画设计自己的集成电路,需要建一个自己的库,方便对自己设计文件的管理。 打开library manager,file 下NEW下Library 选择库的保存路径,一般默认即可,并在name中填入自己库的名字。...

ccbirds 7375

leetcode22. 括号生成 medium

leetcode22. 括号生成 medium 题目描述: 给出n代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 例如,给出n = 3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 解题思路: 解法1:用常规的dfs,但是多加一个...

speargod的博客 241

Frida实战:如何逆向分析某电商App的doCommandNative函数(附完整Hook脚本)

本文详细介绍了如何使用Frida逆向分析电商App中的doCommandNative函数,涵盖动态注册识别、SO加固对抗及高级Hook脚本编写等实战技巧。通过完整Hook脚本示例,帮助安全研究人员深入理解JNICLibrary的关键函数逆向分析方法,提升移动应用安全研究能力。

weixin_29327977的博客 617

【小白爬Leetcode22】4.4 括号生成 Generate Parentheses

【小白爬Leetcode22】4.4 括号生成 Generate Parentheses题目Discription中文描述思路 回溯算法 Leetcode 22 medium\color{#FF0000}{medium}medium 题目 Discription Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given

weixin_41604040的博客 172

VMware Workstation 17 Pro 保姆级教程:手把手教你安装银河麒麟 Kylin V10 SP3 国产系统

本文提供VMware Workstation 17 Pro安装银河麒麟Kylin V10 SP3的详细教程,涵盖环境准备、虚拟机配置、系统安装及优化全流程。通过分步指导和实用技巧,帮助用户高效搭建国产操作系统开发测试环境,特别适合技术自主可控需求场景下的虚拟化实践。

weixin_30879169的博客 646

XCKU060光纤卡学习资料保存:基于kintex UltraScale XCKU060的双路QSFP+光纤PCIe 卡

基于kintex UltraScale XCKU060的双路QSFP+光纤PCIe 卡 一、板卡概述 本板卡系我司自主研发,基于XilinxUltraScaleKintex系列FPGAXCKU060-FFVA1156-2-I架构,支持PCIEGen3x8模式的高速信号处理板卡,搭配两路40GQSFP+接口,两组64-bitDDR4,每组容量8Gbyte,可稳定运行在2400MT/s。板卡具有自控上电顺序,BP...

3075

【Leetcode】22. 生成括号(Generate Parentheses)

Leetcode - 22 Generate Parentheses (Medium) 题目描述:给定整数 n,生成包含 n 个括号合法字符串的所有组合。 [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 解题思路:结合合法括号的特性。 public List<String> generateParenthe...

厉兵秣码 238

大模型事件抽取学习——论文学习笔记《LLMs Learn Task Heuristics from Demonstrations:......》

本篇博客介绍一篇发表在2024 ACL main上的文章:《LLMs Learn Task Heuristics from Demonstrations: A Heuristic-Driven Prompting Strategy for Document-Level Event Argument Extraction》 方向:大模型事件抽取 本篇博客为个人阅读论文的总结与笔记,在这里进行简单的分享 论文链接:https://arxiv.org/abs/

weixin_53751318的博客 1852

17.括号生成(medium)

示例 1:输入:n = 3输出:["((()))","(()())","(())()","()(())","()()()"]示例 2:输入:n = 1输出:["()"] 提示: * 1 <= n <= 8。一种判断括号是否合法的方法:从左往右遍历,左括号的数量始终大于等于右括号的数量,并且左括号的总数量与右括号的总数量相等。2、然后1结束后,回退,这个时候,第二个左括号所在的if跳出来,然后执行右括号逻辑,添加右括号,然后再次进入递归,发现左括号小于3,然后,情况2就出来了。

doting__的博客 669

图像去雾新思路:手把手教你复现DEA-Net中的“细节增强卷积”与PyTorch实现

本文详细解析了DEA-Net中的'细节增强卷积'(DEConv)与PyTorch实现,通过多分支差分卷积和重参数化技术,显著提升单图像去雾效果。文章从数学原理到代码实现,手把手指导复现这一创新架构,帮助开发者在保持轻量化的同时实现41dB以上的PSNR指标,适用于自动驾驶、遥感监测等关键领域。

weixin_30323631的博客 505

Transformer在量化交易中的应用:从时序预测到策略生成

Transformer模型凭借其强大的自注意力机制,在自然语言处理领域取得了革命性突破。其核心原理是通过并行计算和动态权重分配,有效捕捉序列数据中的长距离依赖关系。这一特性使其在金融时间序列分析中展现出巨大技术价值,能够处理市场数据中的复杂非线性模式和长期关联。在量化交易场景下,Transformer可将价格、成交量等连续数据通过离散化分桶处理,转化为类似文本的序列,进而进行预测或直接生成交易信号。本文以QuantGPT项目为例,探讨了如何将这一前沿AI架构迁移应用于金融领域,涵盖了从数据作为“语言”的编码

weixin_26757949的博客 367

LeetCode 22. 括号生成(Medium)

【题目链接】 题解 括号生成 【最简单易懂的】动态规划 思路 动态规划 代码 class Solution: ### 0113 递归(36 ms,15.1 MB) def generateParenthesis(self, n: int) -> List[str]: if n == 0: return [''] res = [] # 考虑剩下n-1对括号的情况,左边i对,右边n - 1 - i对 for .

newCraftsman的博客 190

5G RF前端架构为什么越来越复杂?从8天线、MIMO、ET、DRX到ACLR/EVM彻底拆开

AdjacentChannelLeakageRatioAdjacentChannelLeakageRatio主信道的功率,有多少泄漏到了隔壁Channel。ACLRPAdjacentPMainACLR=ACLRPMain​PAdjacent​​通常以dBc表达。

weixin_43199439的博客 47

Leetcode OJ 22 Generate Parentheses [Medium]

Leetcode OJ 22 Generate Parentheses [Medium] 题目描述: Given n pairs of parentheses, write a function togenerate all combinations of well-formed parentheses. For example, given n = 3, a solution set

大吱的博客 298

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode:22. Generate Parentheses(Medium)

1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括号组合 例如,n = 3,答案: 3. 解题思路 采用递归的方法:给定的整数为n,定义一个字符串类型变量str用来保存组合。"("的个数记为left,")"的个数记为righ...

aysk1112的博客 154

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode--22--medium--GenerateParentheses

summary: dfs | backtrack package myapp.kit.leetcode.top100liked; import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * * 22 * medium * https://leetcode.com/problem...

huangdingsheng的博客 177

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode-22. 括号生成-Java-medium

LeetCode-22. 括号生成-Java-medium

qq_41829337的博客 213

LeetCode22. Generate Parentheses(括号生成)

22.Generate Parentheses [Medium] Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, givenn= 3, a solution set is: [ "((()))"...

grllery的博客 412

【Leetcode-Medium-22】Generate Parentheses

【Leetcode-Easy-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:[ “((()))”,

LYZ 1503

LeetCode Top 100 Liked Questions 22. Generate Parentheses (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 22. Generate Parentheses (Java版; Medium) 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses...

littlehaes的博客 207

力扣做题笔记

力扣做题笔记 文章目录力扣做题笔记前言一、Easy240. 搜索二维矩阵215. 三数之和215. 数组中的第K个最大元素139. 单词拆分206. 反转链表(easy)11. 盛最多水的容器(medium)19. 删除链表的倒数第 N 个结点20. 有效的括号21. 合并两个有序链表22. 括号生成二、Medium31. 下一个排列33. 搜索旋转排序数组(面试搜狐的时候考过)34. 在排序数组中查找元素的第一个和最后一个位置39. 组合总和 *46. 全排列 *48. 旋转图像 *49. 字母异位词分组

weixin_43845522的博客 1855

LeetCode 22. Generate Parentheses(括号生成) 回溯(DFS)/medium

文章目录1.Description2.Example3.Solution 1.Description 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 2.Example 输入:n = 3 输出:["((()))","(()())","(())()","()(())","()()()"] 输入:n = 1 输出:["()"] 3.Solution 有两种写法: 1.先一直添加括号,添加到不合适的时候再退回来。 2.在添加前直接判断是否该添加、该添加哪边

押切徹的博客 231

[LeetCode] 022. Generate Parentheses (Medium) (C++/Java/Python)

[LeetCode] 022. Generate Parentheses (Medium) (C++/Java/Python)

水果君の日常 2056

Leetcode 22. Generate Parentheses [medium] [java]

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: Consideration For length n string, it is the comb...

inkhk的博客 217

LeetCode_Medium_22. Generate Parentheses

2019.1.31 题目描述: 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: [ "((()))", "(()())", "(...

weixin_41637618的博客 246

[LeetCode][22] Generate Parentheses

[LeetCode][22] Generate Parentheses题解 原题链接 参考自https://leetcode.com/problems/generate-parentheses/discuss/10105/Concise-recursive-C%2B%2B-solution 注意这里是if也就是都会走 第一个情况会第一次走进第二个if得到str是( 第二次进入是直接进有(括号的情...

OCEANtroye的博客 299
上一篇: *leetcode-21-合并两个有序链表-easy
下一篇: leetcode-26-删除排序数组中的重复项-easy
Metralix
博客等级 码龄10年 6粉丝 75原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值