class Solution {
public:
void dfs(int leftnum, int rightnum,const int totpair, vector<string>&ret, string str){
if(leftnum == totpair){
for(int iter = rightnum; iter < totpair; ++iter){
str += ")";
}
ret.push_back(str);
return;
}
dfs(leftnum + 1, rightnum, totpair, ret, (string)(str + "("));
if(leftnum > rightnum){
dfs(leftnum, rightnum + 1, totpair, ret, (string)(str + ")"));
}
return;
}
vector<string> generateParenthesis(int n) {
vector<string> ret;
if(n == 0){
return ret;
}
if(n == 1){
ret.push_back("()");
return ret;
}
string str = "";
dfs( 0, 0, n,ret, str);
return ret;
}
};
leetcode generate parenthese
最新推荐文章于 2020-10-09 13:50:19 发布