022 Generate Parentheses
2016-04-05 23:28
169 查看
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:
Subscribe to see which companies asked this question
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void parenthesis(int num, int cnt, string ans, int n, vector<string>& ans) {
if(num < 0) return ;
if(cnt > n*2) {
if(num == 0) {
ans.push_back(ans);
return;
}
else return;
}
parenthesis(num+1, cnt+1, ans+"(", n, ans);
parenthesis(num-1, cnt+1, ans+")", n, ans);
}
vector<string> generateParenthesis(int n) {
vector<string> ans;
parenthesis(0,0,"", n, ans);
return ans;
}
};
int main() {
return 0;
}
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
Subscribe to see which companies asked this question
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void parenthesis(int num, int cnt, string ans, int n, vector<string>& ans) {
if(num < 0) return ;
if(cnt > n*2) {
if(num == 0) {
ans.push_back(ans);
return;
}
else return;
}
parenthesis(num+1, cnt+1, ans+"(", n, ans);
parenthesis(num-1, cnt+1, ans+")", n, ans);
}
vector<string> generateParenthesis(int n) {
vector<string> ans;
parenthesis(0,0,"", n, ans);
return ans;
}
};
int main() {
return 0;
}
相关文章推荐
- C语言头文件的使用(转载)
- CCF系列之最优灌溉(201412-4)
- TCP/IP基础(一)
- 链表的反转
- WOL*LAN远程换醒命令行方法
- 并查集详解
- 开篇日记
- Stanford机器学习-- 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
- CAS原子操作实现无锁及性能分析
- 3TB硬盘的容量已经超出了传统分区标准的支持
- ios 绘制wav波形图
- 面试题28 字符串排列
- android中可以通过两种方式调用接口发送短信
- [BZOJ1061] [NOI2008] 志愿者招募 - 最小费用最大流
- hdu1395 (数论,暴力求余)
- Guava线程池踩坑记录
- poj 3278 catch the cow
- Duplicate files copied in APK META-INF/notice.txt错误的解决办法
- iOS 画音频波形曲线 根据音频数据版
- type()