[Leetcode] 22. Generate Parentheses 解题报告

ESP32 DW1000 UWB 室内定位系统 详细介绍ESP32 DW1000 UWB 室内定位系统的设计演示demo。 阅读详情

题目:

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 {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> ret;
        if(n == 0)      
            return ret;
        string line;
        generateParenthesis(ret, line, 0, 0, n);
        return ret;
    }
private:
    void generateParenthesis(vector<string>& ret, string& line, int left, int right, int n)
    {
        if(left > n)
            return;
        if(left == n && right == n)
        {
            ret.push_back(line);
            return;
        }
        if(left == right)       // we can only add '('
        {
            line += '(';
            generateParenthesis(ret, line, left + 1, right, n);
            line.pop_back();
        }
        else if(left > right)   // we can add both '(' and ')'
        {
            line += '(';
            generateParenthesis(ret, line, left + 1, right, n);
            line.pop_back();
            line += ')';
            generateParenthesis(ret, line, left, right + 1, n);
            line.pop_back();
        }
    }
};


华为OD机考双机位C卷 - 两个字符串间的最短路径(Java & Python& JS & C/C++ & GO ) 华为OD机考双机位C卷 - 两个字符串间的最短路径(OD上机考试双机位C卷)。提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。 阅读详情

相关推荐

Flotherm XT学习历程

大家好,通过半年的热仿真软件学习使用,最终发现最好用,最好上手的软件就是Flotherm XT,对新手非常友好,但由于软件近几年才发布,网上教程非常少,后面我将在这里持续分析软件使用心得,和大家一起学习。

xiaoredred的博客 6259

Generate Parentheses

From LeetCode No.22; Tier:Medium Content: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example: input:n=3 [ “((()))”, “(()())”, “(())(...

sinat_38486956的博客 200

别再手动量了!用Python+Open3D给BIM模型做‘CT扫描’,自动揪出施工误差(附完整代码)

本文介绍如何利用Python和Open3D技术实现BIM模型的毫米级施工误差自动检测。通过点云预处理、模型配准和偏差分析,系统能自动生成三维差异热力图和验收报告,显著提升施工质量检测效率。特别适合混凝土结构错位和钢结构安装偏差等常见问题的智能分析。

weixin_29284657的博客 243

[LeetCode] 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: [ “((()))”, “(()())”, “(())()”,

hubin00sx的博客 356

抖音直播行为规范考试

抖音直播行为规范考试

Q1642968641的博客 1万+

初级文章【西门子】字符拼接成字符串

针对S7-400极其以下PLC,1200往上据说有专门的块了呢,首先,我是不用库的,需要什么函数会自己写,OK进入正题。 首先,我们有一个DB块,里面有14个char,一个14位的char数组和一个14位的string。如下图。 我们的目的是将char1-char10拼接成一个string。 第一种方式:第一步,将char放进数组。 ​​​​​​​ 相信大家都能看明白,但是我还是要解释解释 L取值2 T赋值到临时变量DBNO(INT) OPN DB [#DBNO]获取DB块,这一句.

NS_Speak(IVAV) 1万+

leetcode解题报告22. Generate Parentheses

leetcode解题报告22. Generate Parentheses题目地址 难度是medium题目描述给定一个整数n,代表左括号的个数,要求返回左括号数为n的所有合法的括号组合字符串。我的思路题目直观来看比较简单,我们可以方便地利用stack的后进先出来判断合法的括号字符串,生成一个合法的括号字符串也比较简单。但是难点在于要遍历生成 所有合法的括号字符串,并返回。其实可以发现生成一个合法的括

sysu_xiaoming的博客 333

【目标检测】基于yolov5的铝型材表面缺陷检测(附代码和数据集,Ubuntu或者Linux系统均可运行)

【目标检测】基于yolov5的铝型材表面缺陷检测(附代码和数据集,Ubuntu或者Linux系统均可运行)

学无止境、积少成多、厚积薄发 2170

LeetCode 22. Generate Parentheses 解题报告

LeetCode 22. Generate Parentheses 解题报告

骆小坑的填坑之旅 708

Hermes Agent 桌面版安装部署完全指南:一步步安装自进化Agent智能体

是由开发的开源自主 AI 智能体(MIT 协议)。它不是绑定在集成编辑器上的编码助手,也不是围绕单一 API 的聊天机器人封装——它是一个越用越强大的自主智能体,拥有独一无二的闭环学习系统特性说明自主技能创建在复杂任务后自动生成可复用的 Skills(技能)技能自我改进使用过程中持续优化已有技能持久化记忆跨会话记住你的项目、偏好和解决方案用户画像建模通过 Honcho 方言式推理,逐步构建对你的深度理解20+ 平台接入。

大模型与Agent智能体 7759

YOLO系列专栏(十二):YOLO26行业落地实战(三):无人机航拍场景——户外巡检(机载边缘计算+YOLO26)

无人机航拍巡检场景,面临机载设备算力/显存有限、高空航拍目标小、户外环境复杂(风力、光线变化)、图像抖动等问题,对模型的轻量化、实时性、小目标检测能力要求极高。YOLO26相比传统模型,在该场景中具备三大核心优势:✅ 极致轻量化:INT8量化+模型剪枝后,模型体积可压缩至5MB以内,显存占用控制在512MB以内,适配Jetson Xavier NX等机载边缘计算设备;✅ 小目标检测能力强:优化检测头与锚框配置,可精准识别高空航拍中的小尺寸目标(如线路断股、绝缘子裂纹),检测准确率高;

qq_36130719的博客 258

[leetcode] 22. Generate Parentheses 解题报告

题目链接:https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a sol

小榕流光的专栏 570

YOLO26改进策略【Neck】| CVPR2025 LCA轻量交叉注意力 跨层级双向引导 + 分层降噪增强,提升微小目标特征表达

简单相加、拼接融合无法建立深浅层双向约束关系,浅层微小目标易被深层背景覆盖,深层全局信息难以修正浅层噪声;普通单路自注意力仅单一层内建模依赖,缺少跨层级特征互引导机制,深浅特征信息割裂;标准多头跨注意力计算量大、参数量高,轻量化单模态检测/分割网络部署困难,无法区分前景目标与背景噪声。为此设计单模态轻量化交叉注意力LCA,构建浅层细节、深层语义双分支双向引导结构,轻量完成跨层级特征交互,同步降噪并强化单模态微小目标特征。

Limiiiing的博客 523

【LeetCode】22. Generate Parentheses 解题报告(Python)

题目分析: 这一题是让输入正整数n表示n对括号,然后输出括号所有匹配数,与【LeetCode】17. Letter Combinations of a Phone Number是有些相似的,因为可能的情况很多用循环没法解决,这时候考虑递归或动态规划,递归的代码比较简单,用递归便可解决。 代码说明: 1、def dfs(res, string, n, l, r):,递归函数的参数意义分别为,返回用的...

L141210113的专栏 618

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode #22 Generate Parentheses 创建括号 解题报告

1 解题思路这道题是创建括号对,在做题之前可以首先看下这个 Leetcode #20 Valid Parentheses 括号验证 解题小节,这道题的意思也就是给一个数值n,问你用n对圆括号()可以怎么组合?首先来说,基本的想法肯定就是递归了,这个很简单,我就不细说,我只再说下什么是合法的括号对 1、无论什么时候”(“的数量都大于等于“)”就这条规则,所以递归的时候你们知道怎么递归了吧?2 原题G

MebiuW的专栏 976

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode 22. Generate Parentheses解题报告(python)

22. Generate Parentheses Generate Parentheses python solution 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a...

Orientliu96的博客 424

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode22. Generate Parentheses

Leetcode22. 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 [ "((()))", "...

magic_jiayu的博客 338

LeetCode 22. Generate Parentheses 题解

本题是回溯算法的经典应用,也是卡塔兰数的经典问题。有效括号的两个条件:数量相等、任何前缀左括号 >= 右括号回溯的剪枝策略:只有右括号数量 < 左括号数量时才添加右括号卡塔兰数:n 对括号的有效组合数为第 n 个卡塔兰数通过本题可以深入理解回溯算法的剪枝技巧,以及卡塔兰数在组合问题中的应用。

cannonjinx的博客 4103

22. Generate Parentheses

题目:Generate Parentheses 生成括号 难度:中等 Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, givenn= 3, a solution set is: [ "((()))...

qq_21963133的博客 257

Leetcode1-100: 22. Generate Parentheses

Leetcode1-100: 22. Generate Parentheses问题描述解题思路代码实现implement 1 Brute forceimplement 2 回溯法implement 3 拆分法复杂度分析分析 问题描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solutio

Y123iwantit的博客 243

LeetCode 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: [ "((()))", "(()())", ...

qq_39827131的博客 132

[leetcode] 22. Generate Parentheses - 括号生成

Description Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 括号生成的Python和C++的实现。

Learning from the mistakes 246

LeetCode 22 — Generate Parentheses(C++ Java Python)

题目:http://oj.leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solut

龙之梦 4673

[LeetCode] Generate Parentheses

https://leetcode.com/problems/generate-parentheses/?tab=Description 这道题虽然最后的代码简单,但是想到怎么做却并不容易。后来也是看了discuss才会做...递归的思路是分别跟踪当前剩余的左括号和右括号数量,然后分出两支: 加一个左括号,并往下递归; 如果剩余右括号数多于剩余左括号数,则加右括号,并往下递归;否则什么也...

Sari_2015的博客 158

leetcode 22. Generate Parentheses

一 题目 Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, givenn= 3, a solution set is: [ "((()))", "(()())", "(())()", "(...

bohu83的博客 182

【刷leetcode,拿Offer-031】22. Generate Parentheses

题目链接:https://leetcode.com/problems/generate-parentheses/ 题面: 22.Generate Parentheses Medium 2848177FavoriteShare Givennpairs of parentheses, write a function to generate all combinations of we...

David_Jett 281
上一篇: [Leetcode] 21. Merge Two Sorted Lists 解题报告
下一篇: [Leetcode] 23. Merge k Sorted Lists 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值