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 ...
随机推荐
- asp.net获取客户真实ip非代理ip:
public string GetUserIP() { string _userIP; if(Request.ServerVariables["HTTP_VIA& ...
- C#DateTime与Unix时间戳的转换
/// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...
- C#使用wkhtmltopdf.exe,HTML页面转化为PDF文档
此文用来记录使用wkhtmltopdf.exe在C#代码中将html转换为PDF的过程: 1,在http://wkhtmltopdf.org/downloads.html 下载wkhtmltopdf. ...
- 2019年Angular7——安装搭建路由
Angular 中文官方:https://www.angular.cn/ 为什么要看Angular?我也不知道,因为公司有个Angular的项目要维护.听说Angular的版本已经到7了.以前没怎么玩 ...
- stm32的ADC外设地址设置算法
摘自:https://wenku.baidu.com/view/e3ce2a215901020207409c15.html### /////////////////////////////////// ...
- 【7001】n阶法雷序列
Time Limit: 10 second Memory Limit: 2 MB 问题描述 对任意给定的一个自然数n(n<=100),将分母小于等于n的不可约的真分数按上升的次序排序, ...
- Python——管理属性(2)
__getattr__和__getattribute__ 眼下已经介绍了特性property和描写叙述符来管理特定属性[參考这里],而__getattr__和__getattribute__操作符重载 ...
- [AngularJS] Using an AngularJS directive to hide the keyboard on submit
Pleasea refer to Link <form ng-submit="foo()" handle-phone-submit> <input type=&q ...
- Linux有问必答:Linux上如何查看某个进程的线程
原创:LCTT https://linux.cn/article-5633-1.html 译者: GOLinux本文地址:https://linux.cn/article-5633-1.html201 ...
- libevent源码分析-介绍、安装、使用
Libevent介绍 安装 样例 Libevent介绍 在include\event2\event.h中有关于Libevent的介绍,这里简单翻译介绍一下: Libevent是以事件为驱动的开发可扩展 ...