当前位置:网站首页>哈希——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;
}
}
边栏推荐
- Publish local images to Alibaba cloud
- 系统管理员需知的 16 个 iptables 使用技巧
- 【Golang】golang中map元素的删除和清空
- Lanqiao cup provincial training camp - commonly used STL
- 这个应该是全网最全的接口测试工具之postman
- Kubernetes Foundation
- 运算放大器 —— 快速复苏笔记[贰](应用篇)
- Xilinx FPGA soft core development process
- Leetcode 112. 路径总和
- Lanqiao cup provincial training camp - stack and recursion
猜你喜欢

What is the charm of CSDN members? What's the use of him?

Fifty lectures of Euler (I)

Fiddler抓包工具总结

Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]

JMeter runtime controller

How to convert word to markdown text

Depth first search and breadth first search of Graphs

STM32+ESP8266+MQTT协议连接阿里云物联网平台

Altium one key automatic BOM

Lanqiao cup provincial training camp - stack and recursion
随机推荐
Self taught software testing talent -- not covered
Kubernetes Foundation
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
The solution of permission denied
What is the charm of CSDN members? What's the use of him?
【Golang】golang实现post请求发送form类型数据函数
RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
[golang] golang implements simple Memcache
HDU5667 Sequence
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
Use of getchar
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
tcp 服务端接收数据处理思路梳理,以及select: Invalid argument报错 笔记
JMeter runtime controller
JMeter interface test steps - Installation Tutorial - script recording - concurrent test
Reprint: getting started with cache coherence
Publish local images to Alibaba cloud
The U.S. Department of Homeland Security launched an investigation into the electronic communication records deleted by the secret service during the riots in the Capitol
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程
【Golang】golang实现urlencode urldecode函数