回溯法 Generate Parentheses,第二次总结
class Solution {
public:
vector<string> ans;
void helper(string& cur, int left, int right, int n)
{
//首先两者都必须小于等于n
// not a must
//这一行剪枝不是必须的,因为按照我们增长的规律,是不会出现非法字符串的。
//if(left > n || right > n || left < right)
// return;
if(left == n && right == n) //左右相等,到终点了 ans数组在这里等你
{
ans.push_back(cur);
return;
}
// 如果左侧比n小,可以添加左侧,也可以添加右侧
if(left < n)
{
cur.push_back('(');
helper(cur, left+, right, n);
cur.pop_back();
if(right < left)
{
cur.push_back(')');
helper(cur,left, right+,n);
cur.pop_back();
}
}
else // left == n
{
cur.push_back(')');
helper(cur, left ,right+, n);
cur.pop_back();
}
}
vector<string> generateParenthesis(int n) {
string tmp;
helper(tmp,,,n);
return ans;
}
};
首先这是一个递归,回溯,在最深的地方判断,满足条件的话,就存储起来。
另外,一个容易忽略的点就是,为什么cur.push_back以后要pop_back?
因为是递归调用,你调用之后,不知道被修改成啥样了。如果每一个深度的函数,都能保证,自己这一层调用完之后,完璧归赵,那么...
需要再研究,这一块还有盲点。
回溯法 Generate Parentheses,第二次总结的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- [Swift]LeetCode22. 括号生成 | Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- C语言题库----指针
1.如果f是一个函数,请说明 f() 和f的意思. f是函数的地址,f()是函数 2.怎样理解数组的下标运算? 先偏移,后取址. 3.int *p,*q; int a[]={10,20,30,40}; ...
- Vue2.5
1.1 创建第一个Vue实例
- saltstack实战1-salt-syndic
Syndic建立在中心Master和Minions之间, 并允许多层分级Syndic, 使Salt拓扑可以变得更为灵活. 那么Syndic是如何工作的? 当前有哪些优势和局限哪? Syndic的优势和 ...
- vn.trader的Ubuntu运行环境搭建教程
作者:量衍投资 转载请注明来源:维恩的派(www.vnpie.com) 准备Ubuntu 建议使用一个新安装干净的Ubuntu环境(如果你一定要使用老环境也行,万一不幸掉坑后再回到这步就好),我这里使 ...
- MariaDB的安装与启动
MariaDB的安装与启动 1.安装前需要删除系统已存在的mysql及mariadb [root@vm172--- ~]# rpm -qa|grep mysql [root@vm172--- ~]# ...
- vue.js安装过程(npm安装)
一.开发环境 vue推荐开发环境: Node.js: JavaScript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nodejs下的包管理器. webpack: 它主要的用途 ...
- win10 家庭版 升级 win10企业版
更改秘钥 我的电脑(右键)->属性-> 更改产品秘钥 -> 96YNV-9X4RP-2YYKB-RMQH4-6Q72D->重启系统 如果秘钥过期了,就百度按时间搜索,总有一个是 ...
- Git在已有的分支上新建个人分支开发
在Dev分支上新建一个分支(可以通过Git TE网页创建) 然后就可以从Source下拉列表中看到新建的分支(new_name1)了. 远程分支创建完成之后,就可以在本机上面使用Git GUI Her ...
- 解决github下载速度慢的问题 ,亲测有效
原文链接 https://blog.csdn.net/tsq292978891/article/details/78260066 解决办法: 手动更改hosts 关于hosts的作用这里就不做声明了. ...
- 20175311 2018-2019-2 《Java程序设计》第7周学习总结
20175311 2018-2019-2 <Java程序设计>第7周学习总结 教材学习内容总结 这一周我主要学习了第八章的内容-常用实用类String类 构造String对象 字符串的并置 ...