Skip to content

Commit beb71ce

Browse files
committed
Add lcci/0809-bracket/solution.cc
1 parent a77909f commit beb71ce

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lcci/0809-bracket/solution.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
vector<string> generateParenthesis(int n) {
4+
vector<string> ans;
5+
6+
function<void(int, int, string&)> dfs = [&](int rem_pa, int in_cnt, string& path) {
7+
if (path.length() == n * 2) {
8+
cout << path << " " << rem_pa << ' ' << in_cnt << endl;
9+
ans.push_back(path);
10+
return;
11+
}
12+
13+
if (rem_pa) {
14+
path.push_back('(');
15+
dfs(rem_pa - 1, in_cnt + 1, path);
16+
path.pop_back();
17+
}
18+
19+
if (in_cnt) {
20+
path.push_back(')');
21+
dfs(rem_pa, in_cnt - 1, path);
22+
path.pop_back();
23+
}
24+
};
25+
26+
string path;
27+
path.reserve(n * 2);
28+
29+
dfs(n, 0, path);
30+
31+
return ans;
32+
}
33+
};
34+

0 commit comments

Comments
 (0)