当前位置:网站首页>【力扣】22.括号生成
【力扣】22.括号生成
2022-08-11 03:44:00 【aigo-2021】
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的括号组合。
示例 1:
输入:n = 3
输出:[“((()))”,“(()())”,“(())()”,“()(())”,“()()()”]
示例 2:
输入:n = 1
输出:[“()”]
提示:
1 <= n <= 8
深度优先遍历:树形结构图
分析:
- 当前左右括号都有大于 0 个可以使用的时候,才产生分支;
- 产生左分支的时候,只看当前是否还有左括号可以使用;
- 产生右分支的时候,还受到左分支的限制,右边剩余可以使用的括号数量一定得在严格大于左边剩余的数量的时候,才可以产生分支;
- 在左边和右边剩余的括号数都等于 0 的时候结算。
代码:
class Solution {
// 做减法
public List<String> generateParenthesis(int n) {
List<String> list= new ArrayList<>();
// 执行深度优先遍历,搜索可能的结果
dfs("", n, n, list);
return list;
}
/** * @param str 当前递归得到的结果 * @param left 左括号还有几个可以使用 * @param right 右括号还有几个可以使用 * @param list 结果集 */
private void dfs(String str, int left, int right, List<String> list) {
// 在递归终止的时候,直接把它添加到结果集即可
if (left == 0 && right == 0) {
list.add(str);
return;
}
// 剪枝(左括号可以使用的个数严格大于右括号可以使用的个数,剪枝)
if (left > right) {
return;
}
if (left > 0) {
dfs(str+ "(", left - 1, right, list);
}
if (right > 0) {
dfs(str + ")", left, right - 1, list);
}
}
}
边栏推荐
- typedef定义结构体数组类型
- 互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
- 音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?
- Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
- Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
- Leetcode 450. 删除二叉搜索树中的节点
- CTO说MySQL单表行数不要超过2000w,为啥?
- [ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
- 浮点数在内存中的存储方式
猜你喜欢
The most unlucky and the luckiest
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
Interchangeable Measurement Techniques - Geometric Errors
阿里低代码框架 lowcode-engine 之自定义物料篇
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
【FPGA】day19-二进制转换为十进制(BCD码)
STC8H开发(十五): GPIO驱动Ci24R1无线模块
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
随机推荐
一次简单的 JVM 调优,学会拿去写到简历里
oracle的基数会影响到查询速度吗?
电力机柜数据监测RTU
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
MySQL数据库存储引擎以及数据库的创建、修改与删除
音频编解码,利用FAAC来实现AAC编码
【FPGA】设计思路——I2C协议
How can users overcome emotional issues in programmatic trading?
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
互换性与测量技术——表面粗糙度选取和标注方法
阿里低代码框架 lowcode-engine 之自定义物料篇
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
KingbaseES有什么办法,默认不读取sys_catalog下的系统视图?
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
怎么删除语句审计日志?
【FPGA】名词缩写
AI + medical: for medical image recognition using neural network analysis