-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path22.cpp
More file actions
25 lines (25 loc) · 685 Bytes
/
22.cpp
File metadata and controls
25 lines (25 loc) · 685 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
class Solution {
public:
vector<string> res;
void backtracking(int n, int curr, string& path, int open, int close) {
if (curr == n * 2) {
res.push_back(path);
return;
}
if (open < n) {
path.push_back('(');
backtracking(n, curr + 1, path, open + 1, close);
path.pop_back();
}
if (close < open) {
path.push_back(')');
backtracking(n, curr + 1, path, open, close + 1);
path.pop_back();
}
}
vector<string> generateParenthesis(int n) {
string path = "";
backtracking(n, 0, path, 0, 0);
return res;
}
};