-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-parentheses.cpp
More file actions
31 lines (26 loc) · 889 Bytes
/
Copy pathgenerate-parentheses.cpp
File metadata and controls
31 lines (26 loc) · 889 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
28
29
30
31
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string track;
backtrack(n, n, track, result);
return result;
}
void backtrack(int left, int right, string& track, vector<string>& result) {
// 如果左括号多,那么说明不合法
if (left > right) return;
// 如果左或者右小于 0 也不合法了
if (left < 0 || right < 0) return;
// 如果左右都等于 0 了,那说明所有括号匹配了,添加到 result 中
if (left == 0 && right == 0) {
result.push_back(track);
return;
}
track.push_back('(');
backtrack(left - 1, right, track, result);
track.pop_back();
track.push_back(')');
backtrack(left, right - 1, track, result);
track.pop_back();
}
};