LeetCode: Generate Parentheses [021]
【称号】
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
【题意】
给定n对括号。输出全部可行的括号组合字符串。
所谓合法。就是能够Valid Parentheses方法评判得到true
【思路】
DFS是生成组合的第一选择。维护一个栈来辅助推断。
依据题意,候选的括号组合为n个左括号和n个右括号,我们要生成一个长度为2n的字符串
字符串每一位上的元素仅仅有两种选择,要么左括号。要么右括号。
在入栈推断的时候须要注意下面几点:
1. 当辅助栈为空时,压入左括号,而不考虑右括号
2. 当辅助栈不为空时。栈顶假设是左括号。我们能够压入左括号,或者选择压入右括号(实际上右括号并不真正压入,而是栈顶的左括号出栈与当前右括号匹配)
3. 当辅助栈不为空时,栈顶假设是右括号,仅仅能压入左括号
【代码】
class Solution {
public:
bool isPair(char left, char right){
if(left=='('&&right==')')return true;
return false;
}
void combination(vector<string>&result, stack<char>st, string comb, int leftPareNum, int rightPareNum){
if(leftPareNum==0&&rightPareNum==0){
result.push_back(comb);
return;
} if(st.empty()){
//假设为空则左括号入栈
if(leftPareNum>0){
st.push('(');
combination(result,st,comb+"(",leftPareNum-1,rightPareNum);
}
}
else{
char stTop=st.top();
if(stTop=='('){
if(leftPareNum>0){
st.push('(');
combination(result, st, comb+"(",leftPareNum-1,rightPareNum);
st.pop();
}
if(rightPareNum>0){
st.pop();
combination(result,st, comb+")",leftPareNum,rightPareNum-1);
}
}
else{
//栈顶为右括号,仅仅能压入左括号
if(leftPareNum>0){
st.push('(');
combination(result,st,comb+"(",leftPareNum-1,rightPareNum);
}
} }
} vector<string> generateParenthesis(int n) {
vector<string>result;
stack<char>st;
combination(result,st,"",n,n);
return result;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode: Generate Parentheses [021]的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
随机推荐
- (嵌入式开发)自己写bootloader之编写第二阶段
内核编译(make)之后会生成两个文件,一个Image,一个zImage,其中Image为内核映像文件,而zImage为内核的一种映像压缩文件,Image大约为4M,而zImage不到2M. ...
- HDU 1996汉诺塔VI
题目: n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于 发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上,即各柱 子从下往上的大小仍保持 ...
- cocos2d-x 3.0 android mk文件 之 自己主动遍历*.cpp文件
还记得上一篇android mk 文件的写法吗?传送门, 我们须要手动去加入 cpp文件.假设cpp一多,那不是要累死? LOCAL_PATH := $(call my-dir) include $( ...
- Java反射学习总结终(使用反射和注解模拟JUnit单元测试框架)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 本文是Java反射学习总结系列的最后一篇了,这里贴出之前文章的链接,有兴趣的可以打开看看. ...
- 搭建MHA问题汇总
1,Can't exec "mysqlbinlog": No such file or directory at /usr/share/perl5/vendor_perl/MHA/ ...
- VS2010制作dll
一.为什么需要dll 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,如ATL.M ...
- 用css3解决移动端页面自适应横屏竖屏的思考
之前对于横屏的webapp做过一些尝试,可是始终不是非常好的解决方式,前段时间又接触了类似的需求,尝试了感觉更好的解决方式. 之前的方法写的博客:移动网页横竖屏兼容适应的一些体会 这里举的样例还是平时 ...
- Havel-Hakimi定理 hdu2454 / poj1695 Havel-Hakimi定理
Havel-Hakimi定理 当年一度热门出如今ACM赛场上的算法. 算法定义: Havel-Hakimi定理主要用来判定一个给定的序列是否是可图的. 2.首先介绍一下度序列:若把图 G 全部顶点的度 ...
- css样式继承规则详解
css样式继承规则详解 一.总结 一句话总结:继承而发生样式冲突时,最近祖先获胜(最近原则). 1.继承中哪些样式不会被继承? 多数边框类属性,比如象Padding(补白),Margin(边界),背景 ...
- 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(7)结构体
一.为什么需要结构体? 为了表示一些复杂的事物,而普通类型无法满足实际需求 二.什么叫结构体? 把一些基本类型组合在一起形成的一个新的复合数据类型叫做结构体. 三.如何定义一个结构体? 第一种方式: ...