当前位置:网站首页>leetcode刷题:哈希表08 (四数之和)
leetcode刷题:哈希表08 (四数之和)
2022-06-26 20:30:00 【涛涛英语学不进去】
第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]
]
比三数多一层循环。
这次依旧是以i为第一个数,i+1为第二个数j,然后j+1为第三个数left,length-1为第四个数right.
i为第一层遍历,此处要注意i的范围为[0,nums.length-4]。
依旧要先排序,j的基准是i+1,且j的范围是[i+1,nums.length-3]。
核心部分双指针不变。
i 和 j 在循环时要注意都要与前一个做比较,相同则跳过,但 j 不能和 i 的元素进行比较。因为这只是位置比较,i 和 j肯定不相遇。
最后要注意的是 left和left+1比。右移
right和right-1比,左移
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
//至少四个数
if (nums.length > 3) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
//升序,如果最小的都比目标值大,那还找啥
if (nums[i] > target && nums[i] >= 0) {
break;
}
// i 为基准
if (i > 0 && i < nums.length - 3 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
int left = j + 1;
int right = nums.length - 1;
System.out.println(i + " " + j);
// j 为第二个元素基准
if (j > i + 1 && nums[j] == nums[j - 1]) {
System.out.println("第二个元素和之前的重复,跳过本次循环");
System.out.println(i + " " + j + " " + left + " " + right);
continue;
}
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
System.out.println(i + " " + j + " " + left + " " + right);
System.out.println("和为" + sum);
if (sum == target) {
System.out.println("等于目标值");
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
System.out.println(result);
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
System.out.println(i + " " + j + " " + left + " " + right);
} else if (sum < target) {
left++;
System.out.println("小于目标值,左指针加加");
} else if ((sum > target)) {
right--;
System.out.println("大于目标值,右指针减减");
}
}
System.out.println(i + " " + j);
}
System.out.println(i);
}
}
return result;
}

边栏推荐
- How to install mysql8.0 database under Windows system? (Graphic tutorial)
- Tiktok practice ~ sharing module ~ short video download (save to photo album)
- Muke 8. Service fault tolerance Sentinel
- SentinelResource注解详解
- Boot indicator monitoring
- Browser event loop
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
- Uni app uses canvas to draw QR code
- 回溯思路详解
- Detailed explanation of retrospective thinking
猜你喜欢

好物推荐:移动端开发安全工具

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

Three basic backup methods of mongodb
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

动态规划111

好物推薦:移動端開發安全工具

分布式ID生成系统

Gee: calculate the maximum and minimum values of pixels in the image area

威胁猎人必备的六个威胁追踪工具

Selection of database paradigm and main code
随机推荐
The goal you specified requires a project to execute but there is no POM
SentinelResource注解詳解
[recommended collection] these 8 common missing value filling skills must be mastered
Six necessary threat tracking tools for threat hunters
515. find the maximum value in each tree row
云计算技术的发展与芯片处理器的关系
[Bayesian classification 2] naive Bayesian classifier
What are the specific steps for opening a stock account? Is it safe to open an account online?
30. 串联所有单词的子串
Tiktok practice ~ sharing module ~ copy short video link
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
JWT operation tool class sharing
Is it safe to open an account for CICC Wealth Online?
Détails de l'annotation des ressources sentinelles
Uni app uses canvas to draw QR code
【贝叶斯分类4】贝叶斯网
这些地区考研太疯狂!哪个地区报考人数最多?
【最详细】最新最全Redis面试大全(70道)
MySQL - subquery usage
SentinelResource注解详解