当前位置:网站首页>22. 括号生成
22. 括号生成
2022-04-23 12:48:00 【anieoo】
原题链接:22. 括号生成
solution:dfs+回溯
可以观察到一个性质,左括号数量永远大于等于右括号数量
然后就是一道简的dfs问题了
class Solution {
public:
vector<string> res; //定义返回值
vector<string> generateParenthesis(int n) {
string str = ""; //保存单个有效括号字符串
dfs(0,0,n,str); //dfs遍历
return res;
}
void dfs(int l,int r,int n,string &str) {
if(l > n || r > n || r > l) return;
if(l == n && r== n){
res.push_back(str);
return;
}
str.push_back('(');
dfs(l + 1,r,n,str);
str.pop_back(); //回溯
str.push_back(')');
dfs(l,r + 1,n,str);
str.pop_back();
}
};
版权声明
本文为[anieoo]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42174306/article/details/124360629
边栏推荐
- Number of nodes of complete binary tree
- 对话PostgreSQL作者Bruce:“转行”是为了更好地前行
- Calculate the past date and days online, and calculate the number of live days
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- No idle servers? Import OVF image to quickly experience smartx super fusion community version
- 【vulnhub靶场】-dc2
- Object.keys后key值数组乱序的问题
- leetcode-791. Custom string sorting
- php生成json处理中文
- 风尚云网学习-input属性总结
猜你喜欢
随机推荐
数据库中的日期时间类型
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
Calculate the past date and days online, and calculate the number of live days
有趣的IDEA插件推荐,给你的开发工作增添色彩
如何防止网站被黑客入侵篡改
洛谷P3236 [HNOI2014]画框 题解
大家帮我看一下这是啥情况,MySQL5.5的。谢了
SSM框架系列——Junit单元测试优化day2-3
How to prevent the website from being hacked and tampered with
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
XinChaCha Trust SSL Organization Validated
ZigBee CC2530 minimum system and register configuration (1)
Buuctf Web [gxyctf2019] no dolls
RT-thread中关键词解释及部分API
PHP generates JSON to process Chinese
Qt一个进程运行另一个进程
C#,二维贝塞尔拟合曲线(Bézier Curve)参数点的计算代码
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
Number of nodes of complete binary tree
Source code analysis of synchronousqueue









