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. 《python for data analysis》第二章,美国1880-2010年出生人口姓名的数据分析

    <利用python进行数据分析>第二章的姓名例子,代码.整个例子的所有代码集成到了一个文件中,导致有些对象名如year同时作为了列名与行名,会打印warning,可分不同的part依次运行 ...

  2. Mysql--基础(一)

    MySQL基础 一.数据库的操作 1.SQL分类: DDL(数据定义语言)   :数据定义语言 - Data Definition Language,用来定义数据库的对象,如数据表.视图.索引等.常用 ...

  3. 学习Python3 试了一下百度OCR和腾讯OCR

    因为有个小功能,需要用一下OCR,所以先找了2家,百度和腾讯,如何开通,如何创建应用获得key等不作说明了 百度的比较简单,引用一个AipOcr全部搞定,代码如下: from aip import A ...

  4. C# 广告

    新建一个XML页面,设置属性 选择架构,勾选下面的目标,确定即可添加广告 广告模板: <?xml version="1.0" encoding="utf-8&quo ...

  5. [转]C#调用C++dll

    本文转载至http://www.cnblogs.com/ysharp/archive/2012/05/25/2517803.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问 ...

  6. 前端表单提交数据~php获取表单内容

    上图代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  7. php 学习资料

    http://blog.csdn.net/woshihaiyong168/article/details/52846205  --队列基本原理的认识

  8. 时间规划在Optaplanner上的实现

    在与诸位交流中,使用较多的生产计划和路线规划场景中,大家最为关注的焦点是关于时间的处理问题.确实,时间这一维度具有一定的特殊性.因为时间是一维的,体现为通过图形表示时,它仅可以通过一条有向直线来表达它 ...

  9. Angular中的服务的使用

    定义公共的方法,使得方法在组件之间共享调用 1. 创建服务命令 ng g service modbus-service # 创建服务到指定目录下 ng g service services/modbu ...

  10. altium designer 制作内部不铺铜的封装,如三极管下面禁止铺铜

    制作封装的时候,按P键或菜单栏中点击place选项点选Polygon Pour Cutout.画一个原件禁止铺铜区域即可.