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 ...
随机推荐
- vue配置路由
1,首先用vue-cli搭建vue项目.这个我就不细说了,详见以前的博客 2,npm安装vue-router 3.打开router文件加下的index.js 4.导入你想跳转的组件. import z ...
- Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)
需求缘起:有人在群里@我:请教群主大神一个问题,spring boot + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...
- [Nuxt] Update State with Vuex Actions in Nuxt.js
You can conditionally add classes to Vue.js templates using v-bind:class. This will help display the ...
- [Angular] Implementing A General Communication Mechanism For Directive Interaction
We have modal implement and now we want to implement close functionality. Becuase we use a structure ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- VC/MFC中为程序定义全局快捷键
VC 2010-05-01 18:01:34 阅读287 评论0 字号:大中小 订阅 1.注册快捷键 在初始化函数,如OnInitDialog() 注册快捷键,代码如下: #define HotKey ...
- 【3005】拦截导弹问题(noip1999)
Time Limit: 3 second Memory Limit: 2 MB 某国为了防御帝国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然他的第一发炮弹能达到任意的高度,但是 ...
- Range锁(也即范围锁)
浅析SQL Server在可序列化隔离级别下,防止幻读的范围锁的锁定问题 本文出处:http://www.cnblogs.com/wy123/p/7501261.html (保留出处并非什么原创作品权 ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- USB 3.0规范中译本 附录
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 附录A 符号编码 表A-1显示了对于数据字符字节到符号的编码. 表 A-2显示了对于特殊符号的编码. R ...