SHARE
TWEET

Untitled

a guest Jun 27th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SuperFather {
  2.  
  3.     public static void main(String[] args) {
  4.         int[] nums = {1, 2, 2, 3, 4, 6, 7, 5};
  5.         int target = 6;
  6.         Arrays.sort(nums);
  7.         System.out.println(sum(target, nums, 0));
  8.     }
  9.  
  10.     private static Set<List<Integer>> sum(int target, int[] nums, int index) {
  11.         Set<List<Integer>> result = new HashSet<>();
  12.         int currentNum = nums[index];
  13.  
  14.         if (currentNum == target) {
  15.             List<Integer> lastList = new LinkedList<>();
  16.             lastList.add(currentNum);
  17.             result.add(lastList);
  18.         } else if (currentNum < target && index < nums.length - 1) {
  19.             result.addAll(sum(target, nums, index + 1));
  20.            
  21.             Set<List<Integer>> leftList = sum(target - currentNum, nums, index + 1);
  22.             leftList.forEach(p -> p.add(currentNum));
  23.             result.addAll(leftList);
  24.         }
  25.        
  26.         return result;
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top