-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path22.cpp
More file actions
27 lines (24 loc) · 733 Bytes
/
22.cpp
File metadata and controls
27 lines (24 loc) · 733 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
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n <= 0)
return vector<string>();
vector<string> res;
string s;
generateParenthesis(s,n,n,res);
return res;
}
private:
void generateParenthesis(string s,int left,int right, vector<string> &res){
if(!left && !right){
res.push_back(s);
return;
}
if(left == right) generateParenthesis(s+'(',left-1,right,res);
else if(left == 0) generateParenthesis(s+')',left,right-1,res);
else {
generateParenthesis(s+')',left,right-1,res);
generateParenthesis(s+'(',left-1,right,res);
}
}
};