【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]]
题目解析
四数之和与上一题三数之和的解题思路是一样的,也能通过暴力遍历和哈希表的方法来进行求解,这两种方法在这里就不过多赘述,可以先从两数之和、三数之和两题开始了解具体的解题思路。
双指针解法
解题思路
题目要求不包含重复的四元组,且双指针一般用于有序数组的情况。首先我们将数组进行排序,然后通过两层循环固定两个元素 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.四数之和的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- [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 ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- [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 ...
- 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
随机推荐
- web博客
欢迎大家来戳一戳
- HttpClientFactory的套路,你知多少?
背景 ASP.NET Core 在 2.1 之后推出了具有弹性 HTTP 请求能力的 HttpClient 工厂类 HttpClientFactory. 替换的初衷还是简单摆一下: ① using(v ...
- 使用移动自适应布局+easy mock实现移动界面的简单实现
一.使用easy mock模拟数据 easy mock链接地址 二.自己写移动自适应布局 自己编写主要是利用rem进行宽度栅格布局: html { /* 相当于一个界面适配器,pc以及移动端都可以进行 ...
- python3 flask shell
python shell来操作flask flask shell 报错: from flask_bootstrap import BootstrapImportError: No module nam ...
- python正则表达式之re模块使用
python第一个正则表达式 https://www.imooc.com/learn/550 r'imooc' Pattern Match result In [2]: import re In [ ...
- view添加阴影
//@mg:masksToBounds必须为NO否者阴影没有效果 // cell.layer.masksToBounds = NO; cell.layer.contentsScale = [UI ...
- 关于PHP命名空间的讨论
什么是命名空间? 根据php.net官方翻译文档描述,命名空间是这样定义的: 什么是命名空间?从广义上来说,命名空间是一种封装事物的方法. 在PHP中,命名空间用来解决在编写类库或应用程序时创建可重用 ...
- nes 红白机模拟器 第8篇 USB 手柄支持
买了一个支持 USB OTG, 蓝牙 连接的 安卓手柄. 接到 ubunto 上 dmesg 可以看到识别出来的信息,内核已经支持了. usb - using uhci_hcd usb - usb - ...
- AX中Json转化成表记录
static void JsonToTable(str _json,Common _Common){ sysdictTable dictTable; TableId ...
- ES6—get 与 set
在类里面可以去定义一些getter和setter,getter可以得到一些东西的方法,setter可以设置东西 class Chef{ constructor(food){ this.food = f ...