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,第二次总结的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  3. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  5. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

  7. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  8. [Swift]LeetCode22. 括号生成 | Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

随机推荐

  1. 明令禁止下,哪些APP在违规获取用户信息?

    2019年4月28日消息  移动互联网时代各大APP大行其道,用户为了更便捷地享受互联网服务,常常需要让渡部分个人信息.在信息获取不透明的情况下,不少APP运营企业出现过度获取乃至违规获取用户信息的情 ...

  2. GanttProject 如何显示今天和项目结束

    GanttProject 如何显示今天和项目结束 GanttProject 在甘特图中可以很直观的看出项目开始和结束. 同时也可以看到今天的. 把今天的时间线打开,默认是关闭.

  3. logback root level logger level 日志级别覆盖?继承?

    1. logback-spring.xml 配置 <appender name="STDOUT" class="ch.qos.logback.core.Consol ...

  4. Linux文件夹文件改英文

    打开终端,在终端中输入命令: export LANG=en_US xdg-user-dirs-gtk-update 跳出对话框询问是否将目录转化为英文路径,同意并关闭. 在终端中输入命令: expor ...

  5. C# 将DataTable转换成list (--分页--) 【Skip--Take】

    将DataTable转换成list 及数据分页: /// <summary> /// 酒店评论列表-分页 /// </summary> /// <param name=& ...

  6. 单例模式实例&多线程应用

    单例模式是指:对于一个类在内存中只能存在唯一一个对象,这种设计模式叫做单例设计模式. 单例设计模式的写法: 1. 设置私有(private)的构造方法. 2. 实例化一个该类的对象作为成员变量,并设置 ...

  7. Vue.js中ref ($refs)用法举例总结

    原文地址:http://www.cnblogs.com/xueweijie/p/6907676.html <div id="app"> <input type=& ...

  8. DiscuzX2.5,X3.0,X3.1,X3.2完整目录结构【模板目录template】

    /template/default/common  公共模板目录全局加载 block_forumtree.htm  DIY论坛树形列表模块 block_thread.htm  DIY帖子模块调用文件 ...

  9. Android Studio中绘制simpleUML类图详细说明及使用

    一.Android Studio中安装simpleUML 1.下载simpleUML jar包 地址为:http://plugins.jetbrains.com/  搜索 simpleUMLCE 2. ...

  10. python 进程/线程/协程 测试

    # Author: yeshengbao # -- coding: utf-8 -- # @Time : 2018/5/24 21:38 # 进程:如一个人拥有分身(分数数最好为cpu核心数)几乎同时 ...