LeetCode 20 Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
还是类似的括号匹配问题,这次是要生成所有可能的匹配组合,思路如下:
首先用迭代法穷举可能出现的所有情况,然后利用已有的isValid()判断是否匹配,进而决定是否储存到返回vector容器中。
class Solution {
public:
vector<string> generateParenthesis(int n)
{
mSize = n * ;
init();
string s;
genpa(-, s);
return ret;
}
bool isValid(string s)
{
stack<char> stk;
int nSize = s.size();
for(int i = ; i!= nSize; ++i)
{
if(s[i] == '{' || s[i] == '(' || s[i]== '[')
{
stk.push(s[i]);
}
else
{
if(stk.empty())
{
return false;
}
char cElem = stk.top();
if(cElem - s[i] ==- || cElem - s[i] == -)
{
stk.pop();
continue;
}
else
{
return false;
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
void genpa(int j, string s)
{
if(j == mSize -)
{
if(isValid(s))
{
ret.push_back(s);
}
return;
}
for(int i = ; i!= ; ++i)
{
genpa(j+, s+album[i]);
}
}
void init(void)
{
album[] = '(';
album[] = ')';
}
private:
int mSize;
vector<string> ret;
char album[];
};
思路类似,多举一反三。
LeetCode 20 Generate Parentheses的更多相关文章
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. 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 ...
随机推荐
- 第43天:事件对象event
一.事件对象事件:onmouseover. onmouseout. onclickevent //事件的对象 兼容写法:var event = event || window.event; event ...
- 【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组
题目描述 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. 输入 第一行包含一个正整数n(1<=n<=200000),表示A串的长度. 第二行包含 ...
- hdu 1115 Lifting the Stone (数学几何)
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- P3074 [USACO13FEB]牛奶调度Milk Scheduling
题目描述 Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes ...
- manacher算法详解+模板 P3805
前言: 记住manacher是一个很简单的算法. 首先我们来了解一下回文字串的定义:若一个字符串中的某一子串满足回文的性质,则称其是回文子串.(注意子串必须是连续的,而子序列是可以不连续的) 那么若给 ...
- Linux查看PCIe版本及速率
Linux查看PCIe版本及速率 PCIE有四种不同的规格,通过下图来了解下PCIE的其中2种规格 查看主板上的PCI插槽 # dmidecode | grep --color "PCI&q ...
- JS详细图解全方位解读this
JS详细图解全方位解读this 对于this指向的理解中,有这样一种说法:谁调用它,this就指向谁.在我刚开始学习this的时候,我是非常相信这句话的.因为在一些情况下,这样理解也还算说得通.可是我 ...
- [洛谷P2463][SDOI2008]Sandy的卡片
题目大意:有$n$个字符串,求这$n$个字符串中最长的相似公共字串,相似的定义是加上一个数后相同 题解:差分,建广义后缀自动机,然后求出每个点在多少个字符串中出现过,若在$n$个中都出现,就更新答案 ...
- [洛谷P3174][HAOI2009]毛毛虫
题目大意:给一棵树,求其中最大的“毛毛虫”,毛毛虫的定义是一条链上分出几条边 题解:把每个点的权值定义为它的度数减一,跑带权直径即可,最后答案加二 卡点:无 C++ Code: #include &l ...
- HDU1561:The more, The Better——题解
http://acm.hdu.edu.cn/showproblem.php?pid=1561 ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允 ...