# LeetCode Combination Sum II

LeetCode Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

• All numbers (including target) will be positive integers.
• The solution set must not contain duplicate combinations.

For example, given candidate set `[10, 1, 2, 7, 6, 1, 5]` and target `8`,
A solution set is:

```[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]```

```class Solution {
public:
void work(vector<vector<int>>& ans, vector<int>& candidates, int idx, vector<int>& sol, int target) {
if (target == 0) {
ans.push_back(sol);
}
else {
for (int i = idx; i < candidates.size(); i++) {
if (candidates[i] > target)break;
if (i != idx && candidates[i] == candidates[i - 1])continue; // i != idx
sol.push_back(candidates[i]);
work(ans, candidates, i + 1, sol, target - candidates[i]); // i + 1
sol.pop_back();
}
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> sol;
sort(candidates.begin(), candidates.end());
work(ans, candidates, 0, sol, target);
return ans;
}
};
```