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 ...
随机推荐
- 年轻By塞缪尔·厄尔曼
年轻,并非人生旅程的一段时光,也并非粉颊红唇和体魄的矫健. 它是心灵中的一种状态,是头脑中的一个意念,是理性思维中的创造潜力,是情感活动中的一股勃勃的朝气,是人生春色深处的一缕东风. 年轻,意味着甘愿 ...
- 【EasyUi】页面设计必学之Layout
接触EasyUi也快一年了.非常多时候都把重心放在实现功能方面.要显示大量数据了就用DataGrid,要实现分页效果了就想着Tabs,如此等等,再接下来就是考虑CSS.js怎样让这个功能实现的更好. ...
- Docker搭建ES
Centos7安装ES 和 Docker搭建ES 文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.linux centos7.x安装ES ...
- ng build --base-href的设定问题
项目构建部署中遇到的问题: 1.不使用hash,如何解决刷新页面404的问题? 说明: root 指定项目地址路径,默认为nginx下的html index 默认访问index文件 try_fil ...
- 初识Visual Studio Code 一.使用Visual Studio Code 开发C# 控制台程序
原文:初识Visual Studio Code 一.使用Visual Studio Code 开发C# 控制台程序 1. 安装.NET Core 安装包下载地址:https://www.microso ...
- python中有关字符串的处理
原文 Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字 ...
- EJBCA在Linux上的安装
在windows上安装为了測试用,装在linux服务器上的由于CN用的ip须要重装.....又是折腾一番,以下介绍一些须要注意的地方 一.所需文件 准备的内容就不说了,參考我的上上篇<EJBCA ...
- swift学习第十三天:类的构造函数
类的构造函数 构造函数的介绍 构造函数类似于OC中的初始化方法:init方法 默认情况下载创建一个类时,必然会调用一个构造函数 即便是没有编写任何构造函数,编译器也会提供一个默认的构造函数. 如果是继 ...
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- js进阶课程ajax简介(ajax是浏览器来实现的)
js进阶课程ajax简介(ajax是浏览器来实现的) 一.总结 1.ajax使用需要服务器支持,比如phpstudy 2.ajax是浏览器支持的功能:ajax有个核心对象XMLHttpRequest, ...