40. Combination Sum II

Back to Homepage   |     Back to Code List


class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates);
        helper(res, candidates, new ArrayList<>(), target, 0);
        return res;
    }

    private void helper(List<List<Integer>> res, int[] candidates,
        List<Integer> tmp, int target, int index) {
        if (target <= 0) {
            if (target == 0) {
                res.add(new ArrayList(tmp));
            }
            return;
        }

        for (int i = index; i < candidates.length; i++) {
            if (i > index && candidates[i] == candidates[i - 1]) continue;
            tmp.add(candidates[i]);
            helper(res, candidates, tmp, target - candidates[i], i + 1);
            tmp.remove(tmp.size() - 1);
        }
    }
}