【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: ...
随机推荐
- Maven初级学习(二)Maven使用入门
序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...
- gspx请求周期(备忘)
- lianjie
数值策划入门:如何确定游戏中的资源价值和定价http://bbs.gameres.com/thread_494366.html 一张常规的RPG游戏地图的制作流程 http://bbs.gameres ...
- PHP get_class_methods函数用法
get_class_methods — 返回由类的方法名组成的数组 说明 array get_class_methods ( mixed $class_name ) 返回由 class_name 指定 ...
- servlet和http请求
1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...
- 在C语言源程序中的格式字符与空格等效
#include <stdio.h> #\ i\ n\ c\ l\ u\ d\ e \ <\ s\ t\ d\ l\ i\ b\ .\ h\ > /* *预处理指令这里换行符会 ...
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events. ...
- maven引入jar包时,一个jar的引入错误,会导致后来的jar包的引入。
maven引入本jar包时,引入失败. 问题是另一个jar没有引入正确.
- CentOS6系升级Python2.7版本
安装前准备 本实例以CentOS6.7为例 [root@E tools]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@E tools]# uname -m x ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...