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:

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

题解: 很容易想到DFS, 采用递归的方式进行,比较难把握的是每次递归的结果用什么来存储,终止条件是什么。

需要注意的是,在每次递归的时候剩余左括号不可能比右括号多,否则就终止。

 class Solution {
public:
vector<string> vi;
void generateOne(int left,int right,string s)
{
if(left==)
{
vi.push_back(s+string(right,')'));
return ;
}
if(left>=right)
generateOne(left-,right,s+'(');
else
{
generateOne(left-,right,s+'(');
generateOne(left,right-,s+')');
}
}
vector<string> generateParenthesis(int n) {
int left=n,right=n;
vi.clear();
generateOne(n,n,"");
return vi;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Generate Parentheses的更多相关文章

  1. leetcode题解 Generate Parentheses

    原文链接:https://leetcode.com/problems/generate-parentheses 给出数字n,求n对括号组成的合法字符串. 刚做出来,敲完代码,修改了一次,然后提交,ac ...

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

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

  3. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  4. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  5. leetcode之 Generate Parentheses

    题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...

  6. [LeetCode] 22. Generate Parentheses 生成括号

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

  7. LeetCode 22. Generate Parentheses

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

  8. 【leetcode】Generate Parentheses

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

  9. 【leetcode】 Generate Parentheses (middle)☆

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

随机推荐

  1. springboot-shiro chapter02——springboot webmvc jsp

    简介:这一节主要涉及spring boot 支持jsp, 由于对spring boot不太熟悉,走了一些弯路. 环境:IDEA15+JDK1.8+Maven3+ 代码: https://git.osc ...

  2. 关于服务器raid的一个记录

    今天下午,在装操作系统的时候,特意的测试了下raid1的性能. 1. 开启操作系统 直接正常开启操作系统,操作系统的硬盘做的是raid1,从而数据写俩份,从而在损坏一张盘之后,另外一张盘并不会收到影响 ...

  3. UGUI BUG

    UNITY UGUI问题:父类使用 GroupLayout,子类使用contentsize filter时,会出现运行时布局重叠,但隐藏后再显示就会好了.

  4. shell 中可以for 循环的时间加减日期格式

    ..};do # LAST_HOUR=`date -d '-${num} hour' +%H` 不可for循环,报格式错误 LAST_HOUR=`date "+%H" -d -${ ...

  5. visjs使用小记-1.创建一个简单的网络拓扑图

    1.插件官网:http://visjs.org/ 2.创建一个简单的网络拓扑图 <!doctype html> <html> <head> <title> ...

  6. 3D Math Keynote

    [3DMathKeynote] 1.常用公式. 1)(A*B)^T = B^T*A^T.   2)(A*B)^-1 = B^-1*A^-1. 3)|A*B| = |A|*|B|. 4)|M^T|=|M ...

  7. fidder 自动保存请求内容

    背景: 因为公司有有app和sdk,这些项目没有对应的接口统计.重构的时候很容易忽略掉.所以对fiddler写了一点代码,能将请求的数据写入到文件或者数据库中.方便统计接口,下次重构的时候,方便统计影 ...

  8. Hibernate多对多操作

    ---------------------siwuxie095 Hibernate 多对多操作 以用户和角色为例 (一)多对多映射配置 第一步:创建两个实体类,用户和角色 第二步:让两个实体类之间互相 ...

  9. H5(1)

    css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又 ...

  10. Python学习笔记_使用openpyxl操作Excel,在同一个文件里复制某一个sheet

    应用场景:定制一个Excel模板文件,其中定义了一个模板Sheet,以此模板文件里的模板sheet为样例,制作报表,里面有不止一个模板样例Sheet 一.软件环境: 1.OS:Win10 64位 2. ...