leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
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 (递归 + 卡特兰数)的更多相关文章
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [leetcode] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
随机推荐
- Android支付接入(一):支付宝
原地址:http://blog.csdn.net/simdanfeg/article/details/9011603 转载之前我想深深地感谢屌丝哥 相信相同过App获取利润的都会需要接入计费SDK,下 ...
- SDUT2190救基友记1
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2190 思路 : 这个题,一开始以为是博弈,以为 ...
- 动态改变QSS
通常,一旦设置使用setObjectName来初始设置QSS: list_widget = new QListWidget(); list_widget->setObjectName(" ...
- ArrayList源代码深入剖析
第1部分 ArrayList介绍ArrayList底层采用数组实现,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, Cloneable, java. ...
- POJ2632——Crashing Robots
Crashing Robots DescriptionIn a modernized warehouse, robots are used to fetch the goods. Careful pl ...
- *gravity的取值详表
android有 android:layout_gravity 和 android:gravity,前者设置相对父控件布局,后者是设置自己内部的控件的布局. Value Description top ...
- windows下的go语言的环境搭建和初探
闲话不说,直入主题. 1.准备工具 a.windows下的Go语言开发安装包 官方下载地址:https://code.google.com/p/go/downloads/list b.Go语言中文官网 ...
- 【HDOJ】3473 Minimum Sum
划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> ...
- 【POJ】2104 K-th Number
区间第K大数.主席树可解. /* 2104 */ #include <iostream> #include <sstream> #include <string> ...
- UML中关系图解
转自http://blog.csdn.net/duran1986/article/details/5573415 最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有 ...