程序媛詹妮弗
终身学习

K Sum模板:

*如果返回特定index,用HashMap
*如果返回组合本身且 K > 2, 无论如何先Arrays.sort(nums), 再降为TwoSum问题(指针对撞)

public int[] twoSumII(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length - 1;
        while (i < j) {
            int sum = numbers[i] + numbers[j];
            if (sum < target) {
                i++;
            } else if (sum > target) {
                j--;
            } else {
                return new int[]{i + 1, j + 1};
            }
        }
        return null;
    }

  

K Sum高频题:

 [Leetcode]167. Two Sum II - Input array is sorted 两数之和II

public int[] twoSumII(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length - 1;
        while (i < j) {
            int sum = numbers[i] + numbers[j];
            if (sum < target) {
                i++;
            } else if (sum > target) {
                j--;
            } else {
                return new int[]{i + 1, j + 1};
            }
        }
        return null;
    }

[leetcode]15. 3Sum三数之和

public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        int target = 0;
        Arrays.sort(nums); //先排序
        // corner case
        if (nums.length < 3) return result;

        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;   // skip duplicates
            int j = i + 1;
            int k = nums.length - 1;
            while (j < k) {
                if (nums[i] + nums[j] + nums[k] < target) {
                    j++;
                    while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
                } else if (nums[i] + nums[j] + nums[k] > target) {
                    k--;
                    while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
                } else {
                    result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    j++;
                    k--;
                    while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
                    while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
                }
            }
        }
        return result;
    }

[leetcode]16. 3Sum Closest最接近的三数之和

 public int threeSumClosest(int[] num, int target) {
        int result = 0; 
        int minGap = Integer.MAX_VALUE;
        Arrays.sort(num);

        for (int i = 0; i < num.length - 2; ++i) {
            int j = i + 1;
            int k = num.length - 1;

            while (j < k) {
                int sum = num[i] + num[j] + num[k];
                int gap = Math.abs(sum - target);
                if (gap < minGap) {
                    result = sum;
                    minGap = gap;
                }

                if (sum < target) {
                    j++;
                } else {
                    k--;
                }
            }
        }
        return result;
    }

 [leetcode]259. 3Sum Smaller 三数之和小于目标值

 public int threeSumSmaller(int[] nums, int target) {
        if (nums == null || nums.length == 0) return 0;
        int result = 0;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 2; i++) {
            int j = i + 1;
            int k = nums.length - 1;
            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                if (sum < target) {
                    result += k - j;
                    j++;
                } else {
                    k--;
                }
            }
        }
        return result;
    }

[leetcode]18. 4Sum四数之和

public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums.length < 4) return result;   // corner case
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;   // skip duplicates
            for (int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;  // skip duplicates
                int k = j + 1;
                int l = nums.length - 1;
                while (k < l) {
                    int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    if (sum < target) {
                        k++;
                        while (nums[k] == nums[k - 1] && k < l) k++;  // skip duplicates
                    } else if (sum > target) {
                        l--;
                        while (nums[l] == nums[l + 1] && k < l) l--;  // skip duplicates
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
                        k++;
                        l--;
                        while (nums[k] == nums[k - 1] && k < l) k++;  // skip duplicates
                        while (nums[l] == nums[l + 1] && k < l) l--;   // skip duplicates
                    }
                }
            }
        }
        return result;
    }

  

 

posted on 2020-12-29 05:16  程序媛詹妮弗  阅读(118)  评论(0编辑  收藏  举报