algorithm - subset sum from recursive backtracking -
i trying modify "list combinations in string" question, problem need print sets have given 'sum'. here initial code, not sure how can save list of sets in code:
/** * method return sets given sum. **/ public static list<list<integer>> lists = new arraylist<>(); public static list<integer> list = new arraylist<integer>(); public static void countsubsetsum3(int arr[], int k, int sum) { if(sum == 0) { return; } if(sum != 0 && k == 0) { return; } if(sum < arr[k - 1]) { countsubsetsum3(arr, k-1, sum); } countsubsetsum3(arr, k-1, sum - arr[k-1]); countsubsetsum3(arr, k-1, sum); }
how can modify code make print possible sets have sum 'sum'. thanks!
you have return function when string
's length 0
. right you're printing prefix
, line:
subset(prefix + string.charat(0), string.substring(1));
is executed, raises stringindexoutofboundsexception
because you're calling .substring(1)
on empty string.
Comments
Post a Comment