【leetcode】 Generate Parentheses (middle)☆
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路:产生所有合理的括号配对
我自己用的回溯法,遍历所有压入第k个')'时前面'('个数
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
if(n == )
return ans;
vector<vector<char>> S(n); //放入第k个右括号时,已经放入的左括号数目
int k = ;
for(int i = k + ; i <= n; i++)
{
S[k].push_back(i);
}
string X;
while(k >= )
{
while(!S[k].empty())
{
int numOfLeftP = getNumOfLeft(X); //还没有放入的左括号数目
while(numOfLeftP < S[k].back()) //如果X中"("少于需要的,压入"("
{
X.append("(");
numOfLeftP++;
}
while(numOfLeftP > S[k].back()) //如果X中"("多于需要的,弹出
{
char back = X.back();
X.pop_back();
if(back == '(')
{
numOfLeftP--;
}
}
X.append(")"); //压入新的")"
int back = S[k].back();
S[k].pop_back();
if(k < n - )
{
k++;
int num = max(back, k + ); //可以选择的已有左括号数必须大于当前已有的 小于等于n
for(int i = num; i <= n; i++)
{
S[k].push_back(i);
}
}
else
{
ans.push_back(X);
}
}
k--;
}
return ans;
}
int getNumOfLeft(string X)
{
int position=;
int i=;
while((position=X.find_first_of("(",position))!=string::npos)
{
position++;
i++;
}
return i;
}
};
我的思路很繁琐,中间要做各种判断,看下大神的。
产生长度为 2*n的括号, 但是不能随便产生
设len是当前的字符串长度, v是当前完整配对的括号对数
如果 len - v < n 还可以放入'('
如果 2 * v < len 还可以放入')'
当长度符合条件就压入答案,只有这时stack长度才会减小,其他长度下会压入新值。
vector<string> generateParenthesis2(int n) {
vector<string> ans;
vector<string> stack;
vector<int> validationStack;
stack.push_back("(");
validationStack.push_back();
while(stack.size() != )
{
string s = stack.back();
stack.pop_back();
int v = validationStack.back();
validationStack.pop_back();
if(s.length() == * n)
{
ans.push_back(s);
continue;
}
if(s.length() - v < n)
{
stack.push_back(s.append("("));
validationStack.push_back(v);
s.pop_back();
}
if( * v < s.length())
{
stack.push_back(s.append(")"));
validationStack.push_back(v + );
s.pop_back();
}
}
return ans;
}
【leetcode】 Generate Parentheses (middle)☆的更多相关文章
- 【LeetCode】Generate Parentheses 解题报告
[题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【Leetcode】【Medium】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】【动态规划】Generate Parentheses(括号匹配问题)
描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【LeetCode】Valid Parentheses(有效的括号)
这道题是LeetCode里的第20道题. 题目要求: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- 【leetcode】Valid Parentheses
题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
随机推荐
- 如何确定C#代码是在编译时执行还是在运行时执行
突然想起那个"switch..case..."的case标签都可以判断哪些类型... 就先搞了一个错误的demo... class Program { static void Ma ...
- 【PHP面向对象(OOP)编程入门教程】19.抽象方法和抽象类(abstract)
在OOP语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法做为外部代码访问其的接口.而抽象方法就是为了方便继承而引入的,我们先来看一下抽象类和抽象方法的定义再说明它的用途. 什么是抽象方 ...
- Android开源项目(二)
第二部分 工具库 主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过 ...
- BZOJ1455——罗马游戏
1.题目大意:维护一个数据结构,可以实现合并操作,还能询问最小值 2.分析:这种问题当然是可并堆啦 随便写了一个左偏树QAQ #include <cstdio> #include < ...
- Tomcat7优化配置
导读 Tomcat在使用的过程中会遇到很多报错,有些是程序的报错,但还有一部分是tomcat本身的报错,我们可以通过优化tomcat的初始配置来提高tomcat的性能.Tomcat的优化主要体现在两方 ...
- 热更新脚本C#light,ulua,Scorpio性能比较
http://www.unity蛮牛.com/thread-32861-1-1.html 测试环境: unity4.5.2 三个脚本全是源码导入 PC :处理器 Intel(R) Core(TM) ...
- Android应用签名
http://www.cnblogs.com/ghj1976/archive/2011/07/18/2109381.html 为了要签名? 开发Android的人这么多,完全有可能大家都把类名,包名起 ...
- python解释器快捷键
13. 交互式输入的编辑和历史记录 某些版本的 Python 解释器支持编辑当前的输入行和历史记录,类似于在 Korn shell 和 GNU Bash shell 中看到的功能.这是使用GNU Re ...
- SSH-Struts第二弹:一个Form提交两个Action
根据CSDN中的博客:http://blog.csdn.net/forwayfarer/article/details/3030259进行学习. 1.多个submit的Form表单页面 or 在jsp ...
- 160809225-叶桦汀《C语言程序设计》实验报告
#include<stdio.h> int main() { int a,b,c,t; printf("请输入三个整数"); scanf("%d%d%d&qu ...