My concise solution

  • 0
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        if (candidates.length == 0) return result;
        if (candidates[0] > target) return result;
        takeSum(candidates, target, 0, new LinkedList<Integer>(), 0, result);
        return result;
    public void takeSum(int[] candidates, int target, int index, List<Integer> temp, int pre, List<List<Integer>> result){
        if (pre == target)
            result.add(new LinkedList<Integer>(temp));
        else if (pre < target && index < candidates.length){
            takeSum(candidates, target, index + 1, temp, pre, result);//not take index
            takeSum(candidates, target, index, temp, pre + candidates[index], result);//take index
            temp.remove(temp.size() - 1);

Log in to reply

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