Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement