当前位置:网站首页>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
边栏推荐
猜你喜欢

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

SSM框架系列——数据源配置day2-1

SSL certificate refund instructions
![[daily question] chessboard question](/img/70/54a924d07c935965e54b96d9b07278.png)
[daily question] chessboard question

Please help me see what this is, mysql5 5. Thanks

What are the forms of attack and tampering on the home page of the website
![[unity note] basic lighting in l4unity](/img/38/d88245af2062ed67fb8e61327f3bb9.png)
[unity note] basic lighting in l4unity
![[vulnhub range] - DC2](/img/b7/c34f69a231dad653b8a912f1f36bef.png)
[vulnhub range] - DC2

Plato farm - a game of farm metauniverse with Plato as the goal

Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
随机推荐
Process virtual address space partition
RT-thread中关键词解释及部分API
Bert base Chinese Download (SMART)
21 days learning mongodb notes
Kubernetes 入门教程
Please help me see what this is, mysql5 5. Thanks
梳理網絡IP代理的幾大用途
天梯赛赛前练习
CGC: contractual graph clustering for community detection and tracking
[unity note] basic lighting in l4unity
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
A graphic designer's fantasy world | ones characters
Ad20 supplementary note 3 - shortcut key + continuous update
Object. The disorder of key value array after keys
Source code analysis of synchronousqueue
Analysis of InnoDB execution process in MySQL
只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
数据库中的日期时间类型
No idle servers? Import OVF image to quickly experience smartx super fusion community version