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. 一、linux搭建jenkins+github详细步骤

    事情缘由: 现在在做的主要工作是通过jenkins+postman实现api的自动化测试,想要达到的效果是,api自动化测试定时跑脚本的同时,github有新的代码提交,jenkins会自动检测部署新 ...

  2. subprocess in python3.5

    subprocess 该子模块允许你创建新的流程,连接到它们的输入/输出/错误管道,并获取他们的返回值.该模块打算替换多个旧的模块和功能:os.system   和  os.spawn * 使用sub ...

  3. Django admin 使用多个数据库

    admin是django自带的一个app,那它涉及的是对Model的所有对象进行增删改查,如果model来自多个数据库如何处理呢? 重写admin.ModelAdmin的如下几个方法就好了: clas ...

  4. deep learning and machine learning

    http://blog.csdn.net/xiangz_csdn/article/details/54580053

  5. Python 多进程使用

    进程通信 方式一.共享内存(进程安全,效率高) 共享变量:multiprocessing.Value 共享数组:multiprocessing.Array   方式二.Manager对象:list, ...

  6. Sublime Text:初学者不知道的那些事

    来源:Duchessjojo@译言 我是Sublime Text代码编辑器的忠实粉丝.我和诸多Mac程序员一样,最初使用的是Textmate代码编辑器.在Sublime Text 2发行后,我才开始转 ...

  7. Python格式符说明

    格式化输出 例如我想输出 我的名字是xxxx 年龄是xxxx name = "Lucy"age = 17print("我的名字是%s,年龄是%d"%(name, ...

  8. [Android]RecyclerView添加HeaderView出现宽度问题

    通过getItemViewType方式判断HeaderView方式添加HeaderView的,结果发现有几个界面HeaderView宽度不能满屏. 于是对比了几种布局,发现LinearLayout为根 ...

  9. C#发送和接受POST请求

    1.发送Post请求代码 /// <summary> /// 发起Http请求 /// </summary> /// <param name="flightDa ...

  10. 论immutable不可变性

    什么叫immutable和mutable?简单来讲,一个immutable的对象一旦被创建好,它的状态将不会改变.反过来,如果一个类的实例是immutable的,那么我们把这个类也称作immutabl ...