GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Karteikarten

Combination Sum

Suhas S Englisch 1 Karte

Karten in diesem Stapel (1)

  1. 1Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. [2, 3, 6, 7] and target 7, [ [7], [2, 2, 3] ]public List> combinationSum(int[] candidates, int target) {
    ArrayList> list = new ArrayList<>();
    Arrays.sort(candidates);
    sum_up_recursive(list,new ArrayList<>(),candidates,target,0);
    return list;
    }
    private void sum_up_recursive(ArrayList> list, ArrayList tempList, int[] nums, int remain, int start ){
    if(remain < 0 ) return;
    if(remain == 0) list.add(new ArrayList<>(tempList));
    else{
    for(int i = start; i start && nums[i] == nums[i-1]) continue;//In case of duplicates
    tempList.add(nums[i]);
    sum_up_recursive(list, tempList, nums, remain - nums[i], i);//If you want the same number to be counted for getting the nummber else put i+1
    tempList.remove(tempList.size()-1);
    }
    }
    }

Informationen

Beschreibung
Karteikarten am Combination Sum, erstellt von Suhas S am 20/03/2018.
Erstellt von
Suhas S
Aufrufe
2
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck