1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(), candidates.end()); vector<vector<int>> res; vector<int> cur; backtrack(candidates, cur, res, 0, target); return res; }
void backtrack(vector<int>& candidates, vector<int> cur, vector<vector<int>>& res, int start, int target) { if (target == 0) { res.push_back(cur); return; } if (target < 0) { return; } for (int i=start; i<candidates.size(); ++i) { if (i > start && candidates[i] == candidates[i-1]) { continue; } cur.push_back(candidates[i]); backtrack(candidates, cur, res, i+1, target - candidates[i]); cur.pop_back(); } }
|