Test case : candidates = [1,1], target = 1, the program below is wrong,but it was AC


  • 0
    N
    public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        
        List<List<Integer>> result = new ArrayList<List<Integer>>();
       
        if(candidates == null || candidates.length == 0) {
            return result;   
        }
        //sort
        Arrays.sort(candidates);
        
        //
        helper(result,new ArrayList<Integer>(),candidates,target,0);
        
        return result;
    }
    
    public void helper(List<List<Integer>> result,List<Integer> combination,int[] candidates,int target,int index) {
        
        if(target == 0) {
            result.add(new ArrayList<Integer>(combination));
            return;
        }
        
        for(int i = index ;i < candidates.length ;i ++ ) {
            
            if(target < candidates[i]) {
                return;
            }
            combination.add(candidates[i]);
            helper(result,combination,candidates,target - candidates[i],i);
            combination.remove(combination.size() - 1);
        }
    }
    

    }


  • 0
    N

    result of the program : [[1],[1]]

    right result : [[1]]


Log in to reply
 

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