My Java solution works in IDE but fail here???


  • 1
    H

    this test case [7,3,2], 18

    my solution (below) print out exact expected result on IDE but fail here. Can anyone give me some idea where might be the bug? Thanks

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
    recursiveSum(new ArrayList<Integer>(), candidates, 0, target);
    return result;
    }

    public void recursiveSum(ArrayList<Integer> curList, int[] candidates, int index, int target){
        if(target==0 && index<candidates.length){
        	Collections.sort(curList);
            result.add(new ArrayList<Integer>(curList));
            return;    
        }
        
        if(index == candidates.length) return;
        if(target<0) return;
        
        curList.add(candidates[index]);
        target -= candidates[index];
        recursiveSum(curList, candidates, index, target);
        curList.remove(curList.size()-1);
        target += candidates[index];
    
        recursiveSum(curList, candidates, index+1, target);
    }

Log in to reply
 

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