• 题型总结之K Sum


    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;
        }
    

      

  • 相关阅读:
    strcpy和memcpy的区别《转载》
    C++数组引用
    关于C++中继承、重载、掩盖 《转载》
    对于js原型和原型链继承的简单理解(第三种,复制继承)
    对于js原型和原型链继承的简单理解(第二种,对象冒充)
    腾讯的一道js面试题(原型)
    面试题,自己写写dome总是好的
    对于js原型和原型链继承的简单理解(第一种,原型链继承)
    html+css布局小练习w3cfuns
    C#泛型列表List<T>基本用法总结
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/14204579.html
Copyright © 2020-2023  润新知