递归调用:
1、function函数中的string参数需要定义成const引用;
class Solution {
private:
vector<string> out;
void function(const string &solution, int left, int right, int n){
if(right == n && left == n){
out.push_back(solution);
return;
}
if (left<n && right + 1>left)
function(solution + "(", left + 1, right, n);
else if(left<n && right+1<=left){
function(solution + ")", left, right + 1, n);
function(solution + "(", left + 1, right, n);
}
else
function(solution + ")", left, right + 1, n);
}
public:
vector<string> generateParenthesis(int n) {
string solution;
if(n==0)
return out;
function(solution, 0, 0, n);
return out;
}
};