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的更多相关文章

  1. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  6. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  7. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

随机推荐

  1. python json 序列化

    如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如XML,但更好的方法是序列化为JSON,因为JSON表示出来就是一个字符串,可以被所有语言读取,也可以方便地存储到磁盘或者通过 ...

  2. filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

    filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

  3. 在@Async注解下RequestContextHolder.getRequestAttributes() 获得null的情况

    我们有的时候会在service层获取request填充一些诸如用户名和IP地址等信息,这个时候如果不想从Controller层传request,可以在service直接使用 HttpServletRe ...

  4. Android <Android应用开发实战> 资源类型<二>

    1.菜单资源菜单不仅可以在onCreateContextMenu或onCreateOptionsMenu方法中通过代码创建,还可以在res/menu目录中建立相应的菜单资源文件,并在上面两个方法中加载 ...

  5. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  6. React Patterns

    Contents Stateless function JSX spread attributes Destructuring arguments Conditional rendering Chil ...

  7. [NOI2017]蔬菜——时光倒流+贪心

    题目链接 题解: 貌似一眼看过去是一个贪心. 其他的算法要记录的东西就太多了. 部分分其实很高.但是没有什么提示. 想一些套路:二分?不行还要贪心判断. 分治?前后取法是有影响的. 时光倒流? 也许可 ...

  8. 【线段树】【P3740】 [HAOI2014]贴海报

    传送门 Description Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规 ...

  9. 关于string::size_type

    size_type其实是string模板类定义的一种类型之一,它与size_of的用法相似,只是它根据存储的类型返回字符串的长度.对于 string具体化,将根据char返回字符串的长度,在这种情况下 ...

  10. stout代码分析之六:Stopwatch

    在进行性能测试时,经常需要计算某个函数执行的时长.stout中的Stopwatch类可实现纳秒精度的计时. Stopwatch内部使用timespec记录开始和技术时间.   timeval和time ...