Generate Parentheses
来源:互联网 发布:数据库实施工程师 编辑:程序博客网 时间:2024/05/17 03:20
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> & solution, string & singleSolution,
int numofleft, int numofright, int n){
if (singleSolution.size()==2*n)
solution.push_back(singleSolution);
else{
if (numofleft<n){
singleSolution += '(';
helper(solution, singleSolution, numofleft+1, numofright, n);
singleSolution = singleSolution.substr(0, singleSolution.size()-1);
}
if (numofright<numofleft){
singleSolution += ')';
helper(solution, singleSolution, numofleft, numofright+1, n);
singleSolution = singleSolution.substr(0, singleSolution.size()-1);
}
}
}
vector<string> generateParenthesis(int n) {
vector<string> solution;
string singleSolution = "";
helper(solution, singleSolution, 0, 0, n);
return solution;
}
};
0 0
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- Generate Parentheses
- 在网页标题中加入图片
- 学会缩小搜索条件的范围以便在 Outlook 中更好地搜索
- iphone应用在ipad上的启动画面设置
- js 实现拖动层效果
- js取小数点后两位 方法总结
- Generate Parentheses
- spring mvc 配置(xml配置详解)
- 算法基础——1.3假设修正法
- apache 开启 rewrite 重写规则 .
- axure rp快速原型设计工具
- import-module的注意事项与NDK_MODULE_PATH的配置
- DEM格式转换
- 通讯录联系人各属性获取
- 接口回调方法刷新自定义布局