Simple Java AC Solution


  • 0
    H
    public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        return search(candidates, target, 0);
    }
    private List<List<Integer>> search(int[] candidates, int target, int start) {
        List<List<Integer>> res = new ArrayList();
    	if (target < 0) return null;
    	if (target == 0) {
    		List<Integer> temp = new ArrayList();
    		res.add(temp);
    		return res;
    	}
    	for (int i = start; i < candidates.length; i++) {
    		List<List<Integer>> next = search(candidates, target-candidates[i], i);
            if (next != null) {
                for (List<Integer> t : next) t.add(0, candidates[i]);
                res.addAll(next);
            }
    	}
        return res;
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.