40 - Combination Sum II
Written on October 21, 2015
Tweet
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.
public class Solution {
public List<List<Integer>> combinationSum2(int[] num, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) return result;
Arrays.sort(num);
helper(num, 0, target, new ArrayList<Integer>(), result);
return result;
}
public void helper (int[] num, int start, int remain, List<Integer> list, List<List<Integer>> result) {
if (remain == 0) {
result.add(new ArrayList<Integer>(list));
return;
}
for (int i = start; i < num.length; i++) {
if (i > start && num[i] == num[i - 1]) continue;
if (remain - num[i] >= 0) {
list.add(num[i]);
helper(num, i + 1, remain - num[i], list, result);
list.remove(list.size() - 1);
}
}
}
}
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if not candidates:
return []
candidates.sort()
ret = []
self.helper(candidates, target, [], ret)
return ret
def helper(self, candidates, remain, curr, ret):
if remain == 0:
ret.append(curr[:])
return
for i, num in enumerate(candidates):
if i > 0 and num == candidates[i - 1]:
continue
if num <= remain:
curr.append(num)
self.helper(candidates[i + 1:], remain - num, curr, ret)
curr.pop()