回溯法 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 ...
随机推荐
- Android游戏引擎总汇 原文出处:http://software.intel.com/en-us/blogs/2012/03/13/game-engines-for-android?page=1
随着Android系统的使用越来越广泛,了解一下Android平台下的游戏引擎就非常有必要.而同时因为基于Intel x86的移动设备越来越多,我也非常关注支持x86的移动游戏引擎.然而就目前为止游戏 ...
- c#函数地址传入c++
c# private delegate void ValidateEvent(int eventCode); private static void ValidateEventCallback(int ...
- 用户id,组id和文件访问权限
实际用户ID和实际组ID:标示了我们究竟是谁,这两个字段在登录时取自口令文件中的登录项 有效用户ID和有效组ID以及附属组ID:决定了我们的文件的访问权限(通常有效用户ID等于实际用户ID,有效组ID ...
- 基于MATLAB搭建的DDS模型
基于MATLAB搭建的DDS模型 说明: 累加器输出ufix_16_6数据,通过cast切除小数部分,在累加的过程中,带小数进行运算最后对结果进行处理,这样提高了计算精度. 关于ROM的使用: 直接设 ...
- nginx 配置说明
======nginx 配置文件分开==== http://blog.csdn.net/baple/article/details/44197981 1.备份现在有nginx.conf2.复制ngin ...
- request&response
/* request&response 1.什么是HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP ...
- webuploader分片上传
屁话不多说直接上主题; webuploader,sj(WebUploader 0.1.6)网上有下 powerUpload.js 自己写的基与楼上的插件 asp.net mvc/Api 实现效果: H ...
- Centos 6.9 install Python3.7
# install python3sudo yum -y updatesudo yum -y install yum-utils yum install -y zlib-devel bzip2-dev ...
- 使用COM打开Excel文档注意事项
本文主要讲解程序中打开Excel文档,读写Excel文档可以参照前章: C#读写Excel实践笔记 C#使用NPOI读写Excel的注意事项 如果只是单纯的打开Excel文档,建议使用: System ...
- TCP/IP各层对应的协议
应用层: 该层包括所有和应用程序协同工作,利用基础网络交换应用程序专用的数据协议.如: HTTP:超文本传输协议. TELNET:(网络电传),通过一个终端(terminal)登录到网络(运行在TCP ...