class Solution {
public:
vector<vector<int>> res={};
void helper(vector<int>& candidates, int target, int begin, vector<int> curres){
if (target == 0) res.push_back(curres);
if (target > 0){
for (int i = begin; i<candidates.size() && candidates[i] <= target;){
int nxti = i+1;
while(nxti<candidates.size() && candidates[nxti-1] == candidates[nxti]) nxti++;
curres.push_back(candidates[i]);
helper(candidates, target-candidates[i], i+1, curres);
curres.pop_back();
i = nxti;
}
}
return;
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
helper(candidates, target, 0, vector<int>());
return res;
}
};