-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode22.cpp
37 lines (37 loc) · 933 Bytes
/
leetcode22.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
class Solution {
public:
vector<string> ans;
void dfs(int left,int right,string nowString){
if(left == 0){
nowString += ')';
right--;
if(right == 0){
ans.push_back(nowString);
nowString = "";
return;
}
else{
dfs(left,right,nowString);
}
}
else{
if(left < right){
string temp = nowString+")";
dfs(left,right-1,temp);
temp = nowString+'(';
dfs(left-1,right,temp);
}
else if(left == right){
string temp = nowString+'(';
left--;
dfs(left,right,temp);
}
}
return;
}
vector<string> generateParenthesis(int n) {
string now = "";
dfs(n,n,now);
return ans;
}
};