22. Generate Parentheses

problem

solution

option 1 - dfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<string> ret;
void backtracking(int l, int r, string path){
// 目前為止右括號用的比左括號多
if(l>r) return ;
if(l==0 && r==0){
ret.push_back(path);
return;
}
if(l< 0 || r<0 ) return ;
backtracking(l-1, r,path+'(' );
backtracking(l, r-1, path+')');
}
vector<string> generateParenthesis(int n) {
string path;
backtracking(n,n, path);
return ret;

}
};

option 2

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
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> ret;
vector<int> diff;
ret.push_back("");
diff.push_back(0);
for(int i=0;i<n*2;++i){
vector<string> temp1;
vector<int> temp2;
for(int j=0;j<ret.size() ;++j){
string s = ret[j];
int k = diff[j];

if(i<2*n-1){
temp1.push_back(s+"(");
temp2.push_back(k+1);
}

if(k>0 && i<2*n-1 || k==1 && i==2*n-1){
temp1.push_back(s+")");
temp2.push_back(k-1);
}
}
ret= temp1;
diff = temp2;
}
return ret;
}
};