文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description

2. Solution
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
push(0, 0, result, "", n, '(');
return result;
}
private:
void push(int left, int right, vector<string>& result, string s, const int n, char ch) {
s += ch;
if(ch == '(') {
left++;
}
else {
right++;
}
if(left > n || right > n || right > left) {
return;
}
if(s.length() == 2 * n) {
result.push_back(s);
return;
}
push(left, right, result, s, n, '(');
push(left, right, result, s, n, ')');
}
};
网友评论