feat: 22_generate_parentheses
This commit is contained in:
parent
4296e448ec
commit
34e8cbf547
29
22_generate_parentheses/main.cpp
Normal file
29
22_generate_parentheses/main.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> generateParenthesis(int n) {
|
||||
vector<string> result;
|
||||
|
||||
function<void(string, char, int, int)> recursive = [&](string s, char next, int leftCount, int totalLeftCount) {
|
||||
s.push_back(next);
|
||||
|
||||
if (s.length() == n * 2) {
|
||||
result.emplace_back(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (totalLeftCount < n) {
|
||||
recursive(s, '(', leftCount + 1, totalLeftCount + 1);
|
||||
}
|
||||
if (leftCount > 0) {
|
||||
recursive(s, ')', leftCount - 1, totalLeftCount);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
recursive("", '(', 1, 1);
|
||||
return result;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user