题目描述

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

题目解析

四数之和与上一题三数之和的解题思路是一样的,也能通过暴力遍历和哈希表的方法来进行求解,这两种方法在这里就不过多赘述,可以先从两数之和、三数之和两题开始了解具体的解题思路。

双指针解法

解题思路

题目要求不包含重复的四元组,且双指针一般用于有序数组的情况。首先我们将数组进行排序,然后通过两层循环固定两个元素 nums[i]和nums[j] ,此时就把问题转化为求三数之和。接着通过双指针l和r遍历数组,加快找到 nums[i]+nums[j]+nums[l]+nums[r]=target 的四元组。

代码示例

Java:

public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length < 4) {
return res;
}
Arrays.sort(nums);
int len = nums.length; for (int i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int min = nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
if (min > target) {
break;
}
int max = nums[i] + nums[len - 1] + nums[len - 2] + nums[len - 3];
if (max < target) {
continue;
} for (int j = i + 1; j < len - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int min2 = nums[i] + nums[j] + nums[j + 1] + nums[j + 2];
if(min2 > target){
break;
}
int max2 = nums[i] + nums[j] + nums[len - 1] + nums[len - 2];
if(max2 < target){
continue;
} int l = j + 1, r = len - 1;
while (l < r) {
int sum = nums[i] + nums[j] + nums[l] + nums[r];
if (sum == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[l] ,nums[r]));
while (l < r && nums[l] == nums[++l]);
while (l < r && nums[r] == nums[--r]);
} else if (sum < target) {
l++;
} else {
r--;
}
}
}
}
return res;
}

复杂度分析

时间复杂度:O(n^3)

空间复杂度:O(1)

【LeetCode】18.四数之和的更多相关文章

  1. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  2. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

  3. [Leetcode 18]四数之和 4 Sum

    [题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...

  4. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

  5. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  6. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  7. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  8. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

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

随机推荐

  1. VUE深入浅出(学习过程)

    VUE 2020年02月26日06:27:10 复习过Java8新特性之后开始学习VUE. 了解node了之后,来了解一下VUE.针对于学习VUE用什么开发工具这个问题上,我这里有vsCode和web ...

  2. 第一篇:注册中心Eureka

    1.什么是Eureka,有什么用? Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是spri ...

  3. python3 flask shell

    python shell来操作flask flask shell 报错: from flask_bootstrap import BootstrapImportError: No module nam ...

  4. MongoDB Compass最新版(v_1.20.5)远程连接数据库

    最近下载了最新版本的MongoDB Compass(v_1.20.5)后才发现软件较之前的版本有了很大的变化,主要体现在创建连接页面和连接方式上. 这是旧版的连接页面,所有的参数项以表单的形式列出,直 ...

  5. 钉钉小程序不用canvas在后端绘图前端用image标签获取图片的实践

    公司的需求要用电子员工卡代替用了N久的工作证,在各种场合刷二维码来代替刷卡.在钉钉小程序里实现.感觉这回又要躺坑里了. 钉钉小程序第一次做.我这个自封的GDI+大神才不要想用钉钉jsapi的方式用ca ...

  6. 关于使用fastjson出现的问题:com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 1, fastjson-version 1.2.44

    先说下需求:是从redis中根据keys批量获取数据集合,再通过fastjson转为对象集合 代码如下: 在postman测试后,出现错误如下: 刚开始以为是使用fstjson方法不对,后面先通过打断 ...

  7. Node的require和module.exports

    node编程中最重要的思想之一就是模块,在 Node.js 模块系统中,每个文件都被视为独立的模块.这是这个思想,让javascript的大规模工程成为可能.模块化编程在前端大肆盛行,在node中导出 ...

  8. PyQt完整入门教程

    1.GUI开发框架简介 19年来,一直在做Android ROM相关测试,也有了一定的积累:20年,计划把之前完整的测试方案.脚本.工具进行整合复用. 第一期计划是开发一个GUI的测试工具,近期也进行 ...

  9. py2.7 批量转换文件为 utf8 编码

    source insight 不支持 utf8 ,但是在 linux 上查看的时候是 utf8 编码,就会显示不正常,所以写了个 python 小脚本,可以批量转换 py2.7 #coding:utf ...

  10. 精通HTML DOM

    DOM 1. 属性方法 类型/返回类型 说明 nodeName String 节点名称,根据节点的类型而定义 nodeValue string 节点的值,同样根据节点的类型而定义 nodeType s ...