Generate Parentheses
2015-08-12 16:21
393 查看
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:
class Solution {
public:
void helper(vector<string>& svec, string s, int left, int right)
{
if(left > right)
{
return;
}
if(left == 0 && right == 0)
{
svec.push_back(s);
return;
}
if (left > 0)
{
helper(svec, s + '(', left - 1, right);
}
if(right > 0)
{
helper(svec, s + ')', left, right - 1);
}
}
vector<string> generateParenthesis(int n)
{
vector<string> svec;
string s="";
helper(svec, s, n, n);
return svec;
}
};
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution {
public:
void helper(vector<string>& svec, string s, int left, int right)
{
if(left > right)
{
return;
}
if(left == 0 && right == 0)
{
svec.push_back(s);
return;
}
if (left > 0)
{
helper(svec, s + '(', left - 1, right);
}
if(right > 0)
{
helper(svec, s + ')', left, right - 1);
}
}
vector<string> generateParenthesis(int n)
{
vector<string> svec;
string s="";
helper(svec, s, n, n);
return svec;
}
};
相关文章推荐
- JAVA并发编程
- 微信浏览器网页授权JS封装
- 220 Contains Duplicate III
- java邮件发送和短信发送(一)
- win7 下adb server is out of date. killing... 连接手机问题已解决
- android与js交互(二)
- 哈希表(散列表)
- 字符输入,输出问题
- C#中的泛型
- SQLite 日期 & 时间
- Linux系统排查4——网络篇
- C++/CLI托管
- 自定义GridView六宫格
- Java FileInputStream/FileOutputStream的应用 文件读取和写入
- 鼠标放到图片上实现动态的效果
- 解决混淆报错问题-打包签名出现问题的解决方法
- 数据结构Java实现01----算法概述
- 关于C#反射(转载)
- ie6-7 overflow:hidden失效问题的解决方法
- 一个看股票的小工具