-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.cpp
More file actions
46 lines (42 loc) · 873 Bytes
/
GenerateParentheses.cpp
File metadata and controls
46 lines (42 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
void put(vector<char> &sta, int left, int right, vector<string> &ret){
if(left > 0){
sta.push_back('(');
put(sta, left-1, right, ret);
}
if(right>0 and left < right){
sta.push_back(')');
put(sta, left, right-1, ret);
}
if(left == 0 && right == 0){
string str;
for(auto c:sta){
str.push_back(c);
}
ret.push_back(str);
}
sta.pop_back();
}
vector<string> generateParenthesis(int n) {
vector<string> ret;
vector<char> sta;
sta.push_back('(');
put(sta, n-1, n, ret);
return ret;
}
};
int main(int argc, char *argv[]){
Solution sol;
vector<string> res = sol.generateParenthesis(3);
cout << res.size() << endl;
for(auto &s:res){
cout << s << endl;
}
return 0;
}