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

  • 1

    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){
            result.add(new ArrayList<Integer>(curList));
        if(index == candidates.length) return;
        if(target<0) return;
        target -= candidates[index];
        recursiveSum(curList, candidates, index, target);
        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.