/*
dfs回溯
当left>right || left>0 || right>0 时可以减枝
*/
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string>answer;
dfs(answer,"",n,n);
return answer;
}
void dfs(vector<string> &answer,string ans,int left,int right){
if(left>right || left<0 || right<0){
return;
}
if(left==right && left==0){
answer.push_back(ans);
}
dfs(answer,ans+"(",left-1,right);
dfs(answer,ans+")",left,right-1);
}
};
Leetcode: Generate Parentheses
最新推荐文章于 2020-02-06 12:14:11 发布