当前位置:网站首页>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
边栏推荐
- Pre competition practice of TIANTI competition
- If you were a golang interviewer, what questions would you ask?
- Kubernets Getting started tutoriel
- [daily question] chessboard question
- Labels and paths
- Idea的src子文件下无法创建servlet
- bert-base-chinese下载(智取)
- 4.DRF 权限&访问频率&过滤&排序
- A graphic designer's fantasy world | ones characters
- [vulnhub range] - DC2
猜你喜欢
【每日一题】棋盘问题
Can I take the CPDA data analyst certificate for 0 foundation
Kubernets Getting started tutoriel
4.DRF 权限&访问频率&过滤&排序
风尚云网学习-h5的input:type属性的image属性
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
基于卷积神经网络的遥感影像分类识别系统
8 websites that should be known for product development to enhance work experience
QT double buffer drawing
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
随机推荐
mysql中 innoDB执行过程分析
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
Baserecyclerviewadapterhelper realizes pull-down refresh and pull-up loading
洛谷P3236 [HNOI2014]画框 题解
甲辰篇 創世紀《「內元宇宙」聯載》
BUUCTF WEB [GXYCTF2019]禁止套娃
STM32 control stepper motor (ULN2003 + 28byj)
BUUCTF WEB [BJDCTF2020]The mystery of ip
Everything can be expected in the future | one 2022 campus recruitment officially opened
【蓝桥杯】4月17日省赛刷题训练(前3道题)
CGC: contractual graph clustering for community detection and tracking
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
21 days learning mongodb notes
How do traditional enterprises cope with digital transformation? These books give you the answer
BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
QT interprocess communication
教你快速开发一个 狼人杀微信小程序(附源码)
[unity note] basic lighting in l4unity
Analysis of InnoDB execution process in MySQL
Wonderful review | the sixth issue of "source" - open source economy and industrial investment