题目让你求出所有的 n对括号匹配
的情况
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n <= 0)
return {};
vector<string> res;
helper(res, "", n, 0);
return res;
}
void helper(vector<string> &res, string s, int l, int r) {
if(l == 0 && r ==0) {
res.push_back(s);
return ;
}
if(l > 0)
helper(res, s + "(", l-1, r+1);
if(r > 0)
helper(res, s + ")", l, r-1);
}
};