# Combination Sum II

## Question

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.

Have you met this question in a real interview? Yes
Example
For example, given candidate set 10,1,6,7,2,1,5 and target 8,

A solution set is:

[1,7]

[1,2,5]

[2,6]

[1,1,6]

Note
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order.
(ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.


## 题解

Unique Subsets 非常类似。在 Combination Sum 的基础上改改就好了。

### Java

public class Solution {
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
public List<List<Integer>> combinationSum2(int[] num, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
if (num == null) return result;

Arrays.sort(num);
helper(num, 0, target, list, result);

return result;
}

private void helper(int[] nums, int pos, int gap,
List<Integer> list, List<List<Integer>> result) {

if (gap == 0) {
return;
}

for (int i = pos; i < nums.length; i++) {
// ensure only the first same num is chosen, remove duplicate list
if (i != pos && nums[i] == nums[i - 1]) {
continue;
}
// cut invalid num
if (gap < nums[i]) {
return;
}
// i + 1 ==> only be used once
helper(nums, i + 1, gap - nums[i], list, result);
list.remove(list.size() - 1);
}
}
}