Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SuperFather {
- public static void main(String[] args) {
- int[] nums = {1, 2, 2, 3, 4, 6, 7, 5};
- int target = 6;
- Arrays.sort(nums);
- System.out.println(sum(target, nums, 0));
- }
- private static Set<List<Integer>> sum(int target, int[] nums, int index) {
- Set<List<Integer>> result = new HashSet<>();
- int currentNum = nums[index];
- if (currentNum == target) {
- List<Integer> lastList = new LinkedList<>();
- lastList.add(currentNum);
- result.add(lastList);
- } else if (currentNum < target && index < nums.length - 1) {
- result.addAll(sum(target, nums, index + 1));
- Set<List<Integer>> leftList = sum(target - currentNum, nums, index + 1);
- leftList.forEach(p -> p.add(currentNum));
- result.addAll(leftList);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement