Recursive solution for Java


  • 2
    U
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
    	Arrays.sort(candidates);
    	List<List<Integer>> result = new ArrayList<List<Integer>>();
    	combinationSum(candidates, target, result, new ArrayList<Integer>(), 0);
    	return result;
    }
    
    void combinationSum(int[] candidates, int target, List<List<Integer>> result, List<Integer> current, int index) {
        if(target > 0) {
            for(int i=index; i<candidates.length && candidates[i] <= target; i++) {
                if(i == index || candidates[i] != candidates[i-1]) {
                    current.add(candidates[i]);
                    combinationSum(candidates, target-candidates[i], result, current, i+1);
                    current.remove(current.size()-1);
                }
            }
        } else if(target == 0) {
            result.add(new ArrayList<Integer>(current));
        }
    }

  • 0
    H

    could you please explain the meaning of " if(i == index || candidates[i] != candidates[i-1])",thank you!


Log in to reply
 

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