当前位置:网站首页>22. Bracket generation
22. Bracket generation
2022-04-23 12:51:00 【anieoo】
Original link :22. Bracket generation
solution:dfs+ to flash back
A property can be observed , The number of left parentheses is always greater than or equal to the number of right parentheses
Then there's a simple one dfs Problem.
class Solution {
public:
vector<string> res; // Define the return value
vector<string> generateParenthesis(int n) {
string str = ""; // Save a single valid parenthesis string
dfs(0,0,n,str); //dfs Traverse
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(); // to flash back
str.push_back(')');
dfs(l,r + 1,n,str);
str.pop_back();
}
};
版权声明
本文为[anieoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231248410369.html
边栏推荐
- Buuctf Web [bjdctf2020] zjctf, but so
- 如何防止网站被黑客入侵篡改
- 洛谷P3236 [HNOI2014]画框 题解
- Buuctf Web [gxyctf2019] no dolls
- Homomorphic encryption technology learning
- 4.DRF 权限&访问频率&过滤&排序
- How to prevent the website from being hacked and tampered with
- flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
- Please help me see what this is, mysql5 5. Thanks
- CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
猜你喜欢
Remote sensing image classification and recognition system based on convolutional neural network
QT draw text
If you were a golang interviewer, what questions would you ask?
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
风尚云网学习-input属性总结
SSM框架系列——注解开发day2-2
SSM框架系列——Junit单元测试优化day2-3
STM32控制步进电机(ULN2003+28byj)
Try the server for one month for free, and attach the tutorial
Unable to create servlet under SRC subfile of idea
随机推荐
RT-thread中关键词解释及部分API
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
Metalama简介4.使用Fabric操作项目或命名空间
免费试用一个月的服务器,并附上教程
uni-app 原生APP-云打包集成极光推送(JG-JPUSH)详细教程
云原生KubeSphere部署Mysql
Everything can be expected in the future | one 2022 campus recruitment officially opened
【vulnhub靶场】-dc2
STM32控制步进电机(ULN2003+28byj)
leetcode-791. Custom string sorting
数据库中的日期时间类型
SSM框架系列——注解开发day2-2
使用Source Insight查看编辑源代码
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
mysql中 innoDB执行过程分析
21 天学习MongoDB笔记
Object.keys后key值数组乱序的问题
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
Stm32cubeprogrammer basic instructions
只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网