https://leetcode.com/problems/generate-parentheses/

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:

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

该问题和《编程之美》的买票找零问题一样,通过买票找零问题我们可以知道,针对一个长度为2n的合法排列,第1到2n个位置都满足如下规则:左括号的个数大于等于右括号的个数。所以,我们就可以按照这个规则去打印括号:假设在位置k我们还剩余left个左括号和right个右括号,如果left>0,则我们可以直接打印左括号,而不违背规则。能否打印右括号,我们还必须验证left和right的值是否满足规则,如果left>=right,则我们不能打印右括号,因为打印会违背合法排列的规则,否则可以打印右括号。如果left和right均为零,则说明我们已经完成一个合法排列,可以将其打印出来。通过深搜,我们可以很快地解决问题,针对n=2,问题的解空间如下:

class Solution {
public:
void dfs(vector<string>& res, string load, int n, int l, int r) {
if(load.length() == *n) {
res.push_back(load);
return;
} if(l < n) {
dfs(res, load+"(", n, l+, r);
}
if(r+ <= l) {
dfs(res, load+")", n, l, r+);
}
} vector<string> generateParenthesis(int n) {
vector<string> res; res.clear();
if(n == ) return res; string load = "(";
dfs(res, load, n, , );
return res;
}
};

leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)的更多相关文章

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

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

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

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

  3. Java [leetcode 22]Generate Parentheses

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

  4. [LeetCode] 22. Generate Parentheses ☆☆

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

  5. LeetCode 22. Generate Parentheses

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

  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 [Difficulty: Medium]

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

  8. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  9. [leetcode] 22. Generate Parentheses(medium)

    原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...

随机推荐

  1. WCF 在VS中,添加服务引用,地址输入http://ip/Service.svc,点击前往,提示错误,内容如下:

    WCF的service端的webconfig如下: <?xml version="1.0"?> <configuration> <system.ser ...

  2. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务

    http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...

  3. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)

    公司内部同事分享的一篇文章 周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令.供大家参考 1.Jstack 1.1 ...

  4. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  5. 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)

    当 Adobe.Microsoft.Sun 等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 现今存在的开源协议很多,而经过 Open Source Initiative 组织通过批 ...

  6. 概率图模型之有向图与无向图之间的关系 I map D map perfect map(完美图) 概念

    我们已经讨论了有向图和无向图框架下的概率模型,那么我们有必要讨论一下它们二者的关系.

  7. Windows平台下的session0创建进程的问题与解决办法

    很多博客都有记载如何在session0下创建进程的办法,也就是使用CreateProcessAsUser.但是这个要求服务的进程有SE_INCREASE_QUOTA_NAME和SE_ASSIGNPRI ...

  8. vsphere client cd/dvd 驱动器1 正在连接

    esxi 5.1选择了客户端设备,打开控制台后,CD/DVD一直显示“CD/DVD驱动器1 正在连接” 解决方法:vSphere Client客户端问题,关闭和重新打开vSphere Client客户 ...

  9. poj 1416 Shredding Company( dfs )

    我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...

  10. Innodb的启动

    Innodb的启动与关闭 1. 启动函数的流程 innobase_start_or_create_for_mysql 启动的主要步骤包括: 1. 初始化innodb的参数 2. 初始化系统结构 主要结 ...