zoukankan      html  css  js  c++  java
  • 题型总结之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;
        }
    

      

  • 相关阅读:
    weblogic weak_password 复现
    redis 4-unacc 复现
    Django debug page XSS漏洞(CVE-2017-12794)复现
    (CVE-2016-4437)Apache Shiro <=1.2.4 反序列化漏洞复现
    Apache SSI 远程命令执行漏洞复现
    Apache HTTPD 未知后缀解析漏洞复现
    s2-005复现
    05 跨站请求伪造漏洞CSRF
    安全大面
    JAVA基础学习day03--流程控制语句
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/14204579.html
Copyright © 2011-2022 走看看