-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path427.h
34 lines (33 loc) · 887 Bytes
/
427.h
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
class Solution {
public:
/**
* @param n: n pairs
* @return: All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
// write your code here
if(n <= 0) return vector<string>();
vector<string> r;
string tmp;
helper(n, 0, 0, r, tmp);
return r;
}
void helper(int n, int left, int right, vector<string> &v, string &tmp){
if(left == n && right == n){
v.push_back(tmp);
return;
}
if(left < n){
string old_tmp = tmp;
tmp += '(';
helper(n, left+1, right, v, tmp);
tmp = old_tmp;
}
if(left > right && right < n){
string old_tmp = tmp;
tmp += ')';
helper(n, left, right+1, v, tmp);
tmp = old_tmp;
}
}
};