Flashcards
Combination Sum
Flashcards do conjunto (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);
}
}
}
Informações
- Descrição
- FlashCards sobre Combination Sum, criado por Suhas S em 20-03-2018.
- Criado por
- Suhas S
- Visualizações
- 2
- Avaliação