【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: ...
随机推荐
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- POJ 1905 Expanding Rods
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- Mac 上真正替换LiveWriter 的工具 - ecto
Mac 上真正替换LiveWriter 的工具 - ecto 13年开始使用mac.而后想把 windows 替换到.一直在寻找LiveWriter 的工具,至今终于找到 我先感谢这位博主 http: ...
- 循环执行n次的代码
var audio = document.createElement("audio"); var index = 0; audio.src = "piano/3C ...
- iOS开发——项目篇—高仿百思不得姐 05——发布界面、发表文字界面、重识 bounds、frame、scrollView
加号界面(发布模块) 一.点击加号modal出发布模块,创建控件,布局控件1)使用xib加载view,如果在viewDidLoad创建控件并设置frame 那么self.view 的宽高 拿到的是xi ...
- BZOJ4519——[cqoi2016]不同的最小割
0.题意:求两点之间的最小割的不同的总量 1.分析:裸的分治+最小割,也叫最小割树或GH树,最后用set搞一下就好 #include <set> #include <queue> ...
- BZOJ3083——遥远的国度
1.题目大意:三个操作,换根,修改树上的某条路径,查询一个子树的最小值 2.分析:这个其实还是挺好做的,修改树上的某条路径,裸树剖,查询子树的最小值,这个是树剖满足dfs序 那么就是换根了,对吧,其实 ...
- python 数据结构 初学时没太注意却发现很有用的点点滴滴
1. list.extend(L) 将指定列表中的所有元素附加到另一个列表的末尾:相当于a[len(a):] = L. 2. list.pop([i]) 删除列表中指定位置的元素并返回它.如果未指定索 ...
- BZOJ 1511: [POI2006]OKR-Periods of Words
Description 求一个最长周期. Sol KMP. 一个点的最短周期就是 \(i-next[i]\) 此外 \(i-next[next[i]],i-next[next[next[i]]]\) ...
- 取得DIV的ID还是CLASS
无论你想取得DIV的ID还是CLASS 最重要的是找到你想取值的DIV对象.要取得DIV对象的方法有很多.常用的有2个,一个是根据ID,用var div=document.getElementById ...