[HOT 100] 0018. 四数之和

文章目录

      • 1. 题目链接
      • 2. 题目描述
      • 3. 题目示例
      • 4. 解题思路
      • 5. 题解代码
      • 6. 复杂度分析

1. 题目链接


18. 四数之和 - 力扣(LeetCode)


2. 题目描述


给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abcd 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。


3. 题目示例


示例 1 :

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2 :

输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]

4. 解题思路


优化思路也和前面的三数之和中讲的一样,对于 nums[a] 的枚举:

设 s=nums[a]+nums[a+1]+nums[a+2]+nums[a+3]。如果 s>target,由于数组已经排序,后面无论怎么选,选出的四个数的和不会比 s 还小,所以后面不会找到等于 target 的四数之和了。所以只要 s>target,就可以直接 break 外层循环了。

设 s=nums[a]+nums[n−3]+nums[n−2]+nums[n−1]。如果 s

如果 a>0 且 nums[a]=nums[a−1],那么 nums[a] 和后面数字相加的结果,必然在之前算出过,所以无需执行后续代码,直接 continue 外层循环。(可以放在循环开头判断。)

对于 nums[b] 的枚举(b 从 a+1 开始),也同样有类似优化:

设 s=nums[a]+nums[b]+nums[b+1]+nums[b+2]。如果 s>target,由于数组已经排序,后面无论怎么选,选出的四个数的和不会比 s 还小,所以后面不会找到等于 target 的四数之和了。所以只要 s>target,就可以直接 break。

设 s=nums[a]+nums[b]+nums[n−2]+nums[n−1]。如果 s

如果 b>a+1 且 nums[b]=nums[b−1],那么 nums[b] 和后面数字相加的结果,必然在之前算出过,所以无需执行后续代码,直接 continue。注意这里 b>a+1 的判断是必须的,如果不判断,对于示例 2 这样的数据,会直接 continue,漏掉符合要求的答案。


5. 题解代码


未优化版本 :

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        int n = nums.length;

        for(int a = 0; a < n - 3; a++){
            long x = nums[a];
            if(a > 0 && x == nums[a - 1]) continue;

            for(int b = a + 1; b < n - 2; b++){
                long y = nums[b];
                if(b > a + 1 && y == nums[b - 1]) continue;

                int c = b + 1;
                int d = n - 1;
                while(c < d){
                    long sum = x + y + nums[c] + nums[d];
                    if(sum > target) d--;
                    else if (sum < target) c++;
                    else {
                        ans.add(List.of((int) x, (int) y, nums[c], nums[d]));
                        for(c++; c < d && nums[c] == nums[c - 1]; c++); 
                        for(d--; d > c && nums[d] == nums[d + 1]; d--); 
                    }
                }
            }
        }

        return ans;
    }
}

已优化版本 :

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        int n = nums.length;
        for (int a = 0; a < n - 3; a++) { // 枚举第一个数
            long x = nums[a]; // 使用 long 避免溢出
            if (a > 0 && x == nums[a - 1]) continue; // 跳过重复数字
            if (x + nums[a + 1] + nums[a + 2] + nums[a + 3] > target) break; // 优化一
            if (x + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) continue; // 优化二
            for (int b = a + 1; b < n - 2; b++) { // 枚举第二个数
                long y = nums[b];
                if (b > a + 1 && y == nums[b - 1]) continue; // 跳过重复数字
                if (x + y + nums[b + 1] + nums[b + 2] > target) break; // 优化一
                if (x + y + nums[n - 2] + nums[n - 1] < target) continue; // 优化二
                int c = b + 1;
                int d = n - 1;
                while (c < d) { // 双指针枚举第三个数和第四个数
                    long s = x + y + nums[c] + nums[d]; // 四数之和
                    if (s > target) d--;
                    else if (s < target) c++;
                    else { // s == target
                        ans.add(List.of((int) x, (int) y, nums[c], nums[d]));
                        for (c++; c < d && nums[c] == nums[c - 1]; c++) ; // 跳过重复数字
                        for (d--; d > c && nums[d] == nums[d + 1]; d--) ; // 跳过重复数字
                    }
                }
            }
        }
        return ans;
    }
}


6. 复杂度分析

  • 时间复杂度: 排序的时间复杂度为 O(n log n),外层两个循环分别为 O(n²),双指针法是 O(n)。总时间复杂度是 O(n³)。
  • 空间复杂度: 只用了常数空间来存储一些变量,因此空间复杂度是 O(1),除去存储结果的空间。

你可能感兴趣的:(算法,HOT,100)