卡特兰数
百度百科
想象你站在A点,想着(n,n)点前进,只能在对角线下方走,
即向右走的步数要大于等于向上走的步数
然后每次可以选择向右向上走
这样遍历完所有可能的路径就能得到答案
class Solution {
public:
vector<string>ret;
int n=0;
vector<string> generateParenthesis(int n) {
this->n = n;
finds(0,0,"");
return ret;
}
bool finds(int l,int r,string t){
if(l==n && r==n){
ret.push_back(t);
}
if(l<n){
finds(l+1,r, t+"(" );
}
if(r<n && r<l){
finds(l,r+1, t+")" );
}
return true;
}
};