problem:

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

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

一想到要用递归来解决的就有点头疼,因为还没有好好系统的回顾一下递归,总觉得判断什么时候返回很困难。这个是这样的,用l和r分别记录剩下的左括号和右括号。当l和r都为零的时候就是输出了一种正确的可能了。前提是任何时刻,剩下的右括号要比左括号多或者相等,否则返回。其次,r和l都为零的时候,就把temp str记录下来。代码和部分注释如下:

class Solution {
private:
void subGe(int l, int r, string t, vector<string> &ans)
{
if(l > r) // 左代表‘(’右代表‘)’,剩下的左括号要比右括号少才合法
return ;
if (l == && r == )
{ans.push_back(t);return;} // 这是t已经是2n个字符了
if(l > )
subGe(l - , r, t + "(", ans); // 输出一个(,则左括号减一
if(r > )
subGe(l, r - , t + ")", ans);
}
public:
vector<string> generateParenthesis(int n)
{
vector<string> ans;
ans.clear();
if (n <= )
return ans;
subGe(n, n, "", ans);// 初始时,左右括号都是n个
return ans;
}
};

leetcode第21题--Generate Parentheses的更多相关文章

  1. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  2. LeetCode 笔记系列五 Generate Parentheses

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

  3. LeetCode(22)Generate Parentheses

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

  4. leetcode第20题--Valid Parentheses

    Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  5. leetcode个人题解——#22 Generate Parentheses

    思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...

  6. LeetCode第21题:合并两个有序链表

    题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...

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

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

  8. 【LeetCode算法-21】Merge Two Sorted Lists

    LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...

  9. 乘风破浪:LeetCode真题_022_Generate Parentheses

    乘风破浪:LeetCode真题_022_Generate Parentheses 一.前言 关于括号的题目,我们已经遇到过了验证正确性的题目,现在让我们生成合法的括号列表,怎么办呢?想来想去还是递归比 ...

随机推荐

  1. VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径

    说明 $(RemoteMachine) 设置为"调试"属性页上"远程计算机"属性的值.有关很多其它信息,请參见更改用于 C/C++ 调试配置的项目设置. $(R ...

  2. TortoiseGit客户端密钥配置

    为了方便在windows下使用TortoiseGit客户端提交代码,提高开发效率,现对SSH key的配置进行一下说明,亲测可用. 1.安装TortoiseGit,找到开始菜单里TortoiseGit ...

  3. oracle中execute immediate的使用(select/insert/update/delete)(转)

    execute immediate的语法如下: execute immediate 'sql'; execute immediate 'sql_select' into var_1, var_2; e ...

  4. 微设计(www.weidesigner.com)介绍系列文章(三)

    微设计(www.weidesigner.com)是一个专门针对微信公众账号提供营销推广服务而打造的第三方平台. 3.1 优惠券 优惠券是用于微信上与顾客互动的一种营销方式,不仅能够展现自己的产品,更能 ...

  5. 【Android】Android在AlertDialog使用大全

    package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...

  6. C面试题

    1.sizeof()和strlen()使用? 答案: 1.从功能定义,strlen功能,要查找字符串的长度,sizeof功能是用来寻找指定的变量或变量类型的存储器占用 尺寸: 2.sizeof是运算符 ...

  7. Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS

    进入a b 多少努力p, q 使p*q == a && p < q && p >= b 直接大整数分解 然后dfs所有可能的解决方案劫持 #include ...

  8. UVA 10537 - The Toll! Revisited(dijstra扩张)

    UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...

  9. 2机器学习实践笔记(k-最近邻)

    1:算法是简单的叙述说明 由于训练数据样本和标签,为测试数据的示例,从最近的距离k训练样本,此k练样本中所属类别最多的类即为该測试样本的预測标签. 简称kNN.通常k是不大于20的整数,这里的距离通常 ...

  10. Struts1——离BeanUtils看struts其原理1

    在Struts中非常典型的特点就是使用了ActionForm来搜集表单数据,可是搜集到的表单数据所有都是String类型的.假设我们直接拿来使用我们会面临一个非常麻烦的问题就是频繁的类型装换. Str ...