class Solution {
public:
vector<vector<int>>res;
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<int>path;
combination(candidates,target,path,0);
return res;
}
void combination(vector<int>&candidates,int target,vector<int>&path,int sum)
{
if(target==sum)
{
res.push_back(path);
return ;
}
for(int i=0;i<candidates.size();i++)
{
if(candidates[i]+sum<=target&&(path.size()==0||path.size()!=0&&path[path.size()-1]<=candidates[i]))
{
path.push_back(candidates[i]);
combination(candidates,target,path,sum+candidates[i]);
path.pop_back();
}
}
}
};
Combination Sum
最新推荐文章于 2021-02-23 05:50:14 发布