in LeetCode Array Backtracking ~ read.

Combination Sum

39. Combination Sum

Given a set of candidate numbers (C) (without duplicates) 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:
1. All numbers (including target) will be positive integers.
2. 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]
]
public class Solution {  
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (candidates == null || candidates.length == 0) {
            return result;
        }

        Arrays.sort(candidates);
        combinationSumHelper(candidates, target, result, new ArrayList<>(), 0);

        return result;
    }

    void combinationSumHelper(int[] candidates, int target, List<List<Integer>> result, 
        List<Integer> list, int start) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            result.add(new ArrayList<>(list));
            return;
        }

        for (int i = start; i < candidates.length; i++) {
            list.add(candidates[i]);

            combinationSumHelper(candidates, target - candidates[i], result, list, i);
            list.remove(list.size() - 1);
        }
    }
}

Hope this helps,
Michael