Skip to content

Latest commit

 

History

History
26 lines (25 loc) · 912 Bytes

README.md

File metadata and controls

26 lines (25 loc) · 912 Bytes

Combination Sum

We can solve this proble by Backtracking Algorithm, the better solution is like below:

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        backTrack(0, candidates, new ArrayList<Integer>(), target, res);
        return res;
    }
    
    public void backTrack(int beg, int[] candidates, List<Integer> temp, int target, List<List<Integer>> res) {
        if (target == 0) {
            res.add(temp);
        } else {
            for (int i = beg; i < candidates.length; i++) {
                int c = candidates[i];
                if (c <= target) {
                    List<Integer> t = new ArrayList<Integer>(temp);
                    t.add(c);
                    backTrack(i, candidates, t, target - c, res);
                }
            }
        }
    }
}