feat: 22_generate_parentheses

This commit is contained in:
SquidSpirit 2025-02-22 00:36:55 +08:00
parent 4296e448ec
commit 34e8cbf547

View 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;
}
};