【LeetCode·中等】 18.四数之和(4sum)

题目描述

英文版描述

Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

  • 0 <= a, b, c, d < n
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

Example 1:

Input: nums = 1,0,-1,0,-2,2, target = 0

Output: \[-2,-1,1,2,-2,0,0,2,-1,0,0,1]

Example 2:

Input: nums = 2,2,2,2,2, target = 8

Output: \[2,2,2,2]

Constraints:

  • 1 <= nums.length <= 200
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9

英文版地址

leetcode.com/problems/4s...

中文版描述

给你一个由 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

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

示例 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]

提示:

  • 1 <= nums.length <= 200
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9

中文版地址

leetcode.cn/problems/4s...

解题方法

ini 复制代码
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if (nums == null || nums.length < 4) {
            return new LinkedList<>();
        }
        List<List<Integer>> result = new LinkedList<>();
        Arrays.sort(nums);
    
        for (int i = 0; i <= nums.length - 4; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
                break;
            }
            for (int j = i + 1; j <= nums.length - 3; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
                    break;
                }
                int left = j + 1;
                int right = nums.length - 1;
                while (left < right) {
                    long re = nums[i] + nums[j] + nums[left] + nums[right];
                    if (re == target) {
                        List<Integer> sub = new LinkedList<>();
                        sub.add(nums[i]);
                        sub.add(nums[j]);
                        sub.add(nums[left]);
                        sub.add(nums[right]);
                        result.add(sub);
                         left++;
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }
                        right--;
                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                 
                    } else if (re < target) {
                        left++;
                    } else if (re > target) {
                        right--;
                    }

                }
            }

        }
        return result;
    }
}

复杂度分析

  • 时间复杂度:O(n^3),其中 n 是数组的长度。排序的时间复杂度是 O(nlog⁡n),枚举四元组的时间复杂度是 O(n^3),因此总时间复杂度为 O(n^3+nlog⁡n)=O(n^3)
  • 空间复杂度:O(log⁡n),n 是数组的长度。空间复杂度主要取决于排序额外使用的空间。此外排序修改了输入数组 nums,实际情况中不一定允许,因此也可以看成使用了一个额外的数组存储了数组 nums 的副本并排序。
相关推荐
颜酱12 小时前
07 | 把字段与指标同步到 Qdrant(生成阶段)
前端·人工智能·后端
Larcher12 小时前
从“加载模型”界面到端侧推理:拆解一个 React + WebGPU 大模型 Demo
javascript·后端
Larcher12 小时前
从状态快照到惰性初始化:读懂 React useState 的三个关键场景
javascript·人工智能·后端
lazy H12 小时前
Git clone 怎么用?克隆项目及常见问题完整教程
大数据·git·后端·学习·搜索引擎·github
wang090713 小时前
自己动手写一个spring之aop_1
java·后端·spring
神奇小汤圆13 小时前
IDEA 运行报 Command line is too long?别慌,两招搞定(附原理)
后端
SelectDB13 小时前
Apache Doris 4.1 全面增强 Iceberg:支持 UPDATE、MERGE INTO 与 Iceberg V3
后端
Conan在掘金13 小时前
ArkTS 进阶之道(13):ForEach 循环渲染边界——为啥 build 里不能写 for 循环
后端
小poop14 小时前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
妙码生花14 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(四十一):增加管理员账号管理接口
后端·go·gin