zoukankan      html  css  js  c++  java
  • [LeetCode] 18. 四数之和 ☆☆☆(双指针)

    四数之和

    描述

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

    注意:

    答案中不可以包含重复的四元组。

    示例:

    给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

    满足要求的四元组集合为:
    [
    [-1, 0, 0, 1],
    [-2, -1, 1, 2],
    [-2, 0, 0, 2]
    ]

    解析

    和三数只和类似,只是变为4个数。多一层循环。

    先排序,再比较。可以注意的是,可以加上最小最大值的判断,加快循环。

    代码

    static List<List<Integer>> resList = new ArrayList<>();
        public static List<List<Integer>> fourSum(int[] nums, int target) {
            if (null == nums || nums.length < 4) {
                return resList;
            }
            Arrays.sort(nums);
            int length = nums.length;
            for (int i = 0; i <= nums.length - 4; i++) {
                if (i > 0 && nums[i] == nums[i - 1]) {//相同的值不用再计算
                    continue;
                }
                int minVal = nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
                if (minVal > target) {//最小值大于target,直接返回
                    break;
                }
                int maxVal = nums[i] + nums[length - 1] + nums[length -2] + nums[length - 3];
                if (maxVal < target) {//最大值小于target,下次循环
                    continue;
                }
                for (int k = i + 1; k <= nums.length - 3; k++) {
                    if (k > i + 1 && nums[k] == nums[k - 1]) {//相同的值不用再计算
                        continue;
                    }
                    minVal = nums[i] + nums[k] + nums[k + 1] + nums[k + 2];
                    if (minVal > target) {//最小值大于target,直接返回
                        break;
                    }
                    maxVal = nums[i] + nums[k] + nums[length - 1] + nums[length -2];
                    if (maxVal < target) {//最大值小于target,下次循环
                        continue;
                    }
                    int start = k + 1;
                    int end = nums.length - 1;
                    while (start < end) {
                        int nowRes = nums[i] + nums[k] + nums[start] + nums[end];
                        if (target == nowRes) {
                            resList.add(Arrays.asList(nums[i], nums[k], nums[start], nums[end]));
                            start++;
                            while (start < end && nums[start] == nums[start - 1]) {//相同的值不用再计算
                                start++;
                            }
                            end--;
                            if (start < end && nums[end] == nums[end + 1]) {//相同的值不用再计算
                                end--;
                            }
                        } else if (target > nowRes) {
                            start++;
                        } else {
                            end--;
                        }
                    }
                }
            }
            return resList;
        }
  • 相关阅读:
    数据访问层之Repository
    IIS执行原理
    异常分析
    Logger
    JSTL
    Kubernetes
    NET Core WordPress
    net平台的rabbitmq
    MySQL can’t specify target table for update in FROM clause
    EqualsBuilder和HashCodeBuilder
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/12160664.html
Copyright © 2011-2022 走看看