当前位置:网站首页>[Likou] 22. Bracket generation
[Likou] 22. Bracket generation
2022-08-11 03:58: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);
}
}
}
边栏推荐
- MySQL数据库存储引擎以及数据库的创建、修改与删除
- Which one to choose for mobile map development?
- 一文读懂 高性能可预期数据中心网络
- 【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
- The custom of the C language types -- -- -- -- -- - structure
- Multi-serial port RS485 industrial gateway BL110
- Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
- 【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
- Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
- 阿里云发布3大高性能计算解决方案
猜你喜欢

【FPGA】SDRAM

【FPGA】day19-二进制转换为十进制(BCD码)

Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array

Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes

"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing

STC8H development (15): GPIO drive Ci24R1 wireless module

Use jackson to parse json data in detail

【FPGA】day21- moving average filter

leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》

STC8H开发(十五): GPIO驱动Ci24R1无线模块
随机推荐
Binary tree related code questions [more complete] C language
set_new_handler(0)是什么意思?有什么用?
C语言 recv()函数、recvfrom()函数、recvmsg()函数
What is Machine Reinforcement Learning?What is the principle?
Kubernetes集群搭建Zabbix监控平台
Common layout effect realization scheme
A brief analysis of whether programmatic futures trading or manual order is better?
【FPGA】SDRAM
How to learn machine learning?machine learning process
es-head插件插入查询以及条件查询(五)
App Basic Framework Construction丨Log Management - KLog
Echart地图的省级,以及所有地市级下载与使用
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
C language recv() function, recvfrom() function, recvmsg() function
Redis老了吗?Redis与Dragonfly性能比较
Multi-serial port RS485 industrial gateway BL110
移动端地图开发选择哪家?
Getting Started with Raspberry Pi (5) System Backup
Get the length of the linked list
"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing