回溯法 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 ...
随机推荐
- Oracle的安装+PL安装+系统变量配好后重启
服务启动后的样子 第一步安装oracle服务 链接: https://pan.baidu.com/s/1sRu95Vy1arc3gfuH9nH5Wg 提取码: eaxx 复制这段内容后打开百度网盘手机 ...
- 2018-2019-2 20175207 实验一《JAVA开发环境的熟悉》实验报告
目录 使用JDK编译运行简单程序 使用IDEA对程序进行调试 实验练习 产生一个随机数,让用户猜测,猜错了告诉用户是大了还是小了. 并进行测试(正常情况,异常情况,边界情况) 实验总结 使用IDEA编 ...
- Rabbit MQ
前言: MQ 是什么?队列是什么,MQ 我们可以理解为消息队列,队列我们可以理解为管道.以管道的方式做消息传递. 场景: 1.其实我们在双11的时候,当我们凌晨大量的秒杀和抢购商品,然后去结算的时候, ...
- ss - linux网络工具
用以替代netstat 参看 http://www.cnblogs.com/peida/archive/2013/03/11/2953420.html 常用命令: ss -ptl | grep 991 ...
- Laravel笔记--Eloquent 模型
Eloquent 模型 默认继承use Illuminate\Database\Eloquent\Model类. 数据表名称与模型名称约定: 数据库的表名一般使用“蛇形命名法”命名.蛇形命名法要求单词 ...
- 【C++】如何接收函数return返回来的数组元素
转自 https://www.cnblogs.com/Wade-James/p/7965775.html 我们知道return语句作为被调用函数的结束,返回给调用者函数值.一般来说,是返回一个函数值, ...
- php正则表达式提取img alt/title标签并替换
有时我们需要对富文本编辑器中的img标签进行必要的处理以满足网站自身的需要,比如:根据站点关键词对页面内img的alt标签设定关键词,以下为提取并替换alt/title标签内容的正则: $title ...
- iOS安全系列之一:HTTPS
如何打造一个安全的App?这是每一个移动开发者必须面对的问题.在移动App开发领域,开发工程师对于安全方面的考虑普遍比较欠缺,而由于iOS平台的封闭性,遭遇到的安全问题相比于Android来说要少得多 ...
- Kong(v1.0.2)认证
介绍 上游服务(api或微服务)的流量通常由各种Kong的authentication plugins的应用程序和配置控制.由于Kong的服务实体表示您自己的上游服务的一对一映射,所以最简单的场景是在 ...
- 学习笔记之X分钟速成Python3
X分钟速成Python3 https://mp.weixin.qq.com/s/QT5sR0nUKgJYsYgrj2SleA https://learnxinyminutes.com/docs/zh- ...