-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate Parentheses.cpp
76 lines (55 loc) · 1.59 KB
/
Generate Parentheses.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
Generate(n, res);
return res;
}
void Generate(int n, vector<string>& res){
if(n == 0) return;
if(n == 1){
string p = "()";
res.push_back(p);
return;
}
Generate(n-1, res);
int cur_size = res.size();
for(int i = 0; i < cur_size; i++){
string tmp = res[i];
string tmp_ = tmp;
tmp_.insert(tmp_.begin(), '(');
tmp_.insert(tmp_.end(),')');
res.push_back(tmp_);
tmp_ = tmp;
tmp_ += "()";
res.push_back(tmp_);
if( i != cur_size -1){
tmp_ = tmp;
tmp_.insert(tmp_.begin(),')');
tmp_.insert(tmp_.begin(),'(');
res.push_back(tmp_);
}
}
res.erase(res.begin(), res.begin()+cur_size);
return;
}
};*/
// leftnum > rightnum
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
string s;
Generate(res, s, n, n);
return res;
}
void Generate(vector<string>& res, string s, int leftnum, int rightnum){
if(leftnum == 0 && rightnum ==0){
res.push_back(s);
return;
}
if(leftnum > 0) Generate(res, s+'(', leftnum-1, rightnum);
if(rightnum > leftnum && rightnum > 0) Generate(res, s+')', leftnum, rightnum -1);
return;
}
};