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. 枪弹辩驳(弹丸论破)即将登陆PC

    Spike Chunsoft在PSP上的经典推理游戏: 枪弹辩驳1(Danganronpa: Trigger Happy Havoc)即将登陆PC, PC党有福了. 不过我在PSVita上已经玩完了两 ...

  2. Python argparse用法

    import argparse import sys parser = argparse.ArgumentParser(description='this is for test.') parser. ...

  3. 创建一个dynamics 365 CRM online plugin (九) - Context.Depth

    让我们来看看官方文档是怎么讲的 https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide ...

  4. javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX

    javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX 先检查页面语法是否有问题,后在页面的el ...

  5. Laravel5笔记--路由

    路由:简单讲就是定义URL请求转向某个业务逻辑(一般是控制器方法)的方法. 1.路由定义文件: /routes/web.php   //定义web访问的路由 /routes/api.php    // ...

  6. Vue 给对象添加属性

    坑真多,没想到很多小细节都 改了,我添加个属性都 折腾了半天才看明白原因 Vue.set(row,"isEdit",false);   //给row对象新增一个isEdit的属性.

  7. vs2010直接调用av_register_all crash问题

    需要做一个视频导出的功能,通过ffmpeg来实现,vs2010里面引用ffmpeg库的 dll 和 lib 文件 第一步av_register_all就直接crash了, 查了近半天的时间,都快崩溃了 ...

  8. RAMOS测速

    WIN7X64-DDR31600-RAMOS测速,连续读取17GB/S,连续写入10GB/S,4K读写都是1GB/S左右.不错哦.

  9. Director.js

    Director.js 源码 // // Generated on Tue Dec 16 2014 12:13:47 GMT+0100 (CET) by Charlie Robbins, Paolo ...

  10. Unity 三角函数 向量 运算

    其实三维的和二维的基本差不多,一样的运算方式,unity已经把所有的方法都封装起来,主要是理解,能理解了就直接调用了 三角函数 知识点:三角函数基础正玄余玄.三角函数曲线.弧度制和角度制.弧度制和角度 ...