LeetCode22:括号生成
采用回溯思想
class Solution {
public:
vector<string> res;
void bt(int left,int right,string st){
if(left > right){
return;
}
if(left == 0 && right ==0){
res.push_back(st);
}
if(left > 0){
bt(left - 1,right,st+"(");
}
if(right >0){
bt(left, right - 1,st +")");
}
}
vector<string> generateParenthesis(int n) {
bt(n,n,"");
return res;
}
};