当前位置:网站首页>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
边栏推荐
- Plato farm - a game of farm metauniverse with Plato as the goal
- 洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
- Labels and paths
- uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
- How to prevent the website from being hacked and tampered with
- AD20补充笔记3—快捷键+持续更新
- 只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网
- SSL证书退款说明
- No idle servers? Import OVF image to quickly experience smartx super fusion community version
- 【csnote】ER图
猜你喜欢

Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik

8 websites that should be known for product development to enhance work experience

Unlock openharmony technology day! The annual event is about to open!

教你快速开发一个 狼人杀微信小程序(附源码)

Deploying MySQL in cloud native kubesphere

QT double buffer drawing

SSM framework series - annotation development day2-2

A graphic designer's fantasy world | ones characters

在线计算过往日期天数,计算活了多少天

Everything can be expected in the future | one 2022 campus recruitment officially opened
随机推荐
Homomorphic encryption technology learning
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
力扣刷题之完全二叉树的节点个数
Pre competition practice of TIANTI competition
SSM framework series - JUnit unit test optimization day2-3
MySQL function - recursive function
[vulnhub range] - DC2
STM32 control stepper motor (ULN2003 + 28byj)
What are the forms of attack and tampering on the home page of the website
QT interprocess communication
[wechat applet] Z-index is invalid
教你快速开发一个 狼人杀微信小程序(附源码)
0基础可以考CPDA数据分析师证书吗
Deploying MySQL in cloud native kubesphere
unity常见的问题(一)
bert-base-chinese下载(智取)
XinChaCha Trust SSL Organization Validated
数据库中的日期时间类型
Zigbee之CC2530最小系统及寄存器配置(1)
The continuous construction of the Internet industry platform is not only able to collect traffic