#include using namespace std; class Solution { public: vector> combinationSum2(vector &candidates, int target) { vector> res; sort(candidates.begin(), candidates.end()); dfs(candidates, 0, target, res); return res; } private: vector stack; void dfs(vector &candidates, int start, int target, vector> &res) { if (target < 0) { return; } else if (target == 0) { res.push_back(stack); } else { int last = INT_MIN; for (int i = start; i < candidates.size(); i++) { if (last != candidates[i]) { stack.push_back(candidates[i]); dfs(candidates, i + 1, target - candidates[i], res); stack.pop_back(); } last = candidates[i]; } } } };