public:
//重点:leftNum >= rightNum 限制条件
vector<string> generateParenthesis(int n) {
vector<string> rets;
string parenthesis = "";
//2*n个字符;简单的二分即可。
generateParenthesis(n,0,0,parenthesis,rets);
return rets;
}
void generateParenthesis(int n,int leftNum,int rightNum,string parenthesis,vector<string> &rets){
if(n<=0){
rets.push_back(parenthesis);
return ;
}
if(leftNum>rightNum){
if (leftNum<n)
{
generateParenthesis(n,leftNum+1,rightNum,parenthesis+"(",rets);
}
generateParenthesis(n,leftNum,rightNum+1,parenthesis+")",rets);
}
if(leftNum==rightNum && leftNum<n){
generateParenthesis(n,leftNum+1,rightNum,parenthesis+"(",rets);
}
if(leftNum == rightNum && leftNum==n){
rets.push_back(parenthesis);
}
}