Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Combination Sum (Leetcode)
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public List<List<Integer>> combinationSum(int[] candidates, int target){ List<List<Integer>> out = new ArrayList<List<Integer>>(); helper(out,new ArrayList<Integer>(),candidates,target,0); return out; } //[2,3,5,6,7] , 7 public void helper(List<List<Integer>> list,List<Integer> tmplist,int[] candidates,int target,int index){ if(target==0) list.add(new ArrayList<>(tmplist)); if(target<0) return; for(int i=index;i<candidates.length;i++){ tmplist.add(candidates[i]); helper(list,tmplist,candidates,target-candidates[i],i); tmplist.remove(tmplist.size()-1); } } public static void main(String args[]) { System.out.println("Hello, World!"); Rextester obj = new Rextester(); List<List<Integer>> out = obj.combinationSum(new int[]{2,3,6,7},7); for(List<Integer> lst : out){ for(Integer i:lst){ System.out.print(i+" "); } System.out.println(""); } } }
run
|
edit
|
history
|
help
0
Java
Kochergina_1
3.A
Java
Homework
Alllocate and release memory
Binary Tree Max path Sum
test1
collectors and stream
isPrime