class Solution {
public:
vector<vector<int>> res;
void dfs(int start,vector<int>¤tPath,vector<int> weight,int T)
{
if(T<0)
return;
if(T==0)
{
res.push_back(currentPath);
return;
}
for(int i=start;i<=weight.size()-1;i++)
{
currentPath.push_back(weight[i]);
dfs(i+1,currentPath,weight,T-weight[i]);
currentPath.pop_back();
while(i<weight.size()&&weight[i]==weight[i+1]) //关键一步 必须放在这里 否则会例如 17 17 重复 而且不能放在循环前面
i++;
}
return;
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<int> a;
sort(candidates.begin(),candidates.end());
dfs(0,a,candidates,target);
return res;
}
};