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. 思考的工作方式——计划经济or市场经济

    背景:单位成立了技术领先的基础部门.专注于产品规划的技术解决方案部门.产品的发展规划方向.批准的项目和各部门的其他工作方案.工作内容是在这一领域没有问题.毕竟,从过去企业发展的一个部门模型现在是一个功 ...

  2. 记一次tomcat故障排查(转)

    1~1024之间的端口号是保留端口,通常是为特定目的预留的.虽然你的问题不是由于保留端口引起的,但是仍然建议你不要随意使用保留端口作为自定义服务的端口,如果你能早早遵循这一规则压根就不会遇到这个问题. ...

  3. crm创建报告补充导航

    报告导航实现动态交互体验报告. 通过使用各种类型的操作的,报告允许用户导航到特定的报告.Microsoft Dynamics CRM 记录或其它网站 动态钻取到 Microsoft Dynamics ...

  4. Hibernate的一些相关信息

    在没有学习Hibernate之前,我们一直都是用jdbc来连接数据库和操纵数据库.所以在刚接触Hibernate时,我们都有一个疑问,为什么要学Hibernate,jdbc不是挺好的吗?那么接下来就来 ...

  5. [jQuery1.9]Cannot read property ‘msie’ of undefined错误的解决方法

    原文:[jQuery1.9]Cannot read property 'msie' of undefined错误的解决方法 $.browser在jQuery1.9里被删除了,所以项目的js代码里用到$ ...

  6. unity节目素材ProceduralMaterial采用

    有些效果substance物质的.然而,对房地产的材料可以不寻常Material方法调用,必须ProceduralMaterial打电话. using UnityEngine; using Syste ...

  7. Visual Studio Code开发TypeScript

    [Tool] 使用Visual Studio Code开发TypeScript   [Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在 ...

  8. C++ Primer第九章课后编程问题

    1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0J ...

  9. 当<script>中的type等于text/html的妙用

    我们可以在<script>片断中定义一个被JS调用的代码,但代码又不在页面上显示,这时,我们可以使用下面的方法: <script id="commentTemplate&q ...

  10. Linux_下安装MySQL

    linux下mysql 最新版安装图解教程 1.查看当前安装的linux版本 命令:lsb_release -a 如下图所示 通过上图中的数据可以看出安装的版本为RedHat5.4,所以我们需要下载R ...