leetcode

Combination Sum

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

The same repeated number may be chosen from C unlimited number of times.

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.

For example,

given candidate set 2,3,6,7 and target 7,

A solution set is:

[7]

[2, 2, 3]

Sort the input array, then keep adding the elements until the sum is greater than the target value.

public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        if (candidates==null || candidates.length==0) {
            return results;
        }
        Arrays.sort(candidates);

        combinationSum(results, new ArrayList<Integer>(), 0, candidates, target, 0);

        return results;
    }

    //candidates need to be sorted
    public void combinationSum(List<List<Integer>> results, List<Integer> result, int sum, int[] candidates, int target, int level) {
        if (sum>target) {
            return;
        }
        if (sum==target) {
            List<Integer> solution = new ArrayList<Integer>(result);
            results.add(solution);
            return;
        }

        for (int i=level; i<candidates.length; i++) {
            sum = sum + candidates[i];
            result.add(candidates[i]);
            //we pass the current level in, so it will keep adding the current element until it is more than the target
            combinationSum(results, result, sum, candidates, target, i);
            result.remove(result.size()-1);
            sum = sum - candidates[i];
        }    
    }