class Solution {
public:
vector<vector<int>> res;
void getAns(vector<int> &num, vector<int> &path, int curSum, const int& target){
if(curSum > target) return;
if(curSum == target){
res.push_back(path);
} else {
vector<int> search;
for(int i=0;i<num.size();i++){
if(path.size()==0 || path[path.size()-1] <= num[i]){
path.push_back(num[i]);
getAns(num, path, curSum + num[i], target);
path.pop_back();
}
}
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<int> path;
sort(candidates.begin(), candidates.end());
getAns(candidates, path, 0, target);
return res;
}
};
组合之和 -- 牛客网
最新推荐文章于 2023-05-03 10:51:00 发布