Leetcode 22

时间:2022-03-07 12:47:15
//这题感觉不如前两题回溯清楚,还要再看看
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
string add;
DFS(res,add,n,n);
return res;
} void DFS(vector<string>& res,string add,int x,int y){
if(x > y) return;
if(x == &&y == ){
res.push_back(add);
}
else{
if(x > )DFS(res,add+"(",x-,y);
if(y > )DFS(res,add+")",x,y-);
}
}
};