当前位置:网站首页>哈希——18. 四数之和
哈希——18. 四数之和
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
- 四数之和
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
0 <= a, b, c, d < n
a、b、c 和 d 互不相同
nums[a] + nums[b] + nums[c] + nums[d] == target
你可以按 任意顺序 返回答案 。
2 题目示例
示例 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]]
3 题目提示
1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109
4 思路
排序 + 双指针
最朴素的方法是使用四重循环枚举所有的四元组,然后使用哈希表进行去重操作,得到不包含重复四元组的最终答案。假设数组的长度是n,则该方法中,枚举的时间复杂度为O(n4),去重操作的时间复杂度和空间复杂度也很高,因此需要换—种思路。为了避免枚举到重复四元组,则需要保证每一重循环枚举到的元素不小于其上一重循环枚举到的元素,且在同一重循环中不能多次枚举到相同的元素。
为了实现上述要求,可以对数组进行排序,并且在循环过程中遵循以下两点:
- 每—种循环枚举到的下标必须大于上—重循环枚举到的下标;
- 同—重循环中,如果当前元素与上一个元素相同,则跳过当前元素。
使用上述方法,可以避免枚举到重复四元组,但是由于仍使用四重循环,时间复杂度仍是o(n4)。注意到数组已经被排序,因此可以使用双指针的方法去掉一重循环。
使用两重循环分别枚举前两个数,然后在两重循环枚举到的数之后使用双指针枚举剩下的两个数。假设两重循环枚举到的前两个数分别位于下标i和j,其中i<j。初始时,左右指针分别指向下标j+1和下标n―1。每次计算四个数的和,并进行如下操作:
- 如果和等于target,则将枚举到的四个数加到答案中,然后将左指针右移直到遇到不同的数,将右指针左移直到遇到不同的数;
- 如果和小于target,则将左指针右移一位;
- 如果和大于target,则将右指针左移一位。
使用双指针枚举剩下的两个数的时间复杂度是O(n),因此总时间复杂度是o(n3),低于O(n’)。
具体实现时,还可以进行一些剪枝操作:
- 在确定第一个数之后,如果nums[i]+ nums[i+1]+nums[i+2]+ nums[i+3] > target,说明此时剩下的三个数无论取什么值,四数之和一定大于 target,因此退出第—重循环;
- 在确定第一个数之后,如果 nums[i] + nums[n — 3]+nums[n —2] + nums[n ―1]<target,说明此时剩下的三个数无论取什么值,四数之和一定小于target,因此第一重循环直接进入下一轮,枚举nums[i+1];
- 在确定前两个数之后,如果nums[司+ nums[ij]+ numsl[j+1]+ nums[j+2] > target,说明此时剩下的两个数无论取什么值,四数之和一定大于target,因此退出第二重循环;
- 在确定前两个数之后,如果nums[i]+ nums[j]+nums[n — 2]+nums[n ― 1]<target,说明此时剩下的两个数无论取什么值,四数之和一定小于target,因此第二重循环直接进入下一轮,枚举nums[j+1]。
5 我的答案
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> quadruplets = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 4) {
return quadruplets;
}
Arrays.sort(nums);
int length = nums.length;
for (int i = 0; i < length - 3; 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;
}
if ((long) nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
for (int j = i + 1; j < length - 2; 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;
}
if ((long) nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
int left = j + 1, right = length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
quadruplets.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
}
return quadruplets;
}
}
边栏推荐
- 视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
- Paging query of employee information of black maredge takeout
- MySQL query field matches the record of a rule
- [golang] golang implements MD5 encryption function
- 黑马瑞吉外卖之员工信息分页查询
- STM32+ESP8266+MQTT协议连接阿里云物联网平台
- 【Golang】golang实现发送微信服务号模板消息
- HDU5667 Sequence
- [golang] golang implements the URLEncode URLDecode function
- Docker installs 3 master and 3 slave redis clusters
猜你喜欢

Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error

Publish local image to private library

JMeter runtime controller

DevOps及DevOps常用的工具介绍

Text message verification of web crawler
](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)
运算放大器 —— 快速复苏笔记[壹](参数篇)

Blue Bridge Cup provincial match training camp - Calculation of date

【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)

Simply understand MODBUS function code and partition

Xilinx FPGA Microblaze AXI_ IIC usage and experience
随机推荐
Nodejs ctf 基础
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
MySQL paging
[golang] before method of time type in golang
Xilinx FPGA Microblaze AXI_ IIC usage and experience
Directional crawling Taobao product name and price (teacher Songtian)
08【AIO编程】
How to access the code of online customer service system to your website
Xilinx FPGA soft core development process
08 [AIO programming]
HCIP MGRE实验 第三天
Fiddler packet capture tool summary
Classification and introduction of arm and series processors
Use of getchar
简单理解modbus功能码和分区
Leetcode 112. 路径总和
Blue Bridge Cup provincial match training camp - Calculation of date
The solution of permission denied
JPS has no namenode and datanode reasons
2022, the average salary of the soft tester, after reading it, I was instantly cool