当前位置:网站首页>Leetcode: hash table 08 (sum of four numbers)
Leetcode: hash table 08 (sum of four numbers)
2022-06-26 20:49:00 【Taotao can't learn English】
The first 18 topic . Sum of four numbers
The question : Given an inclusion n Array of integers nums And a target value target, Judge nums Are there four elements in a,b,c and d , bring a + b + c + d The value of is equal to target equal ? Find all quadruples that meet the conditions and are not repeated .
Be careful :
The answer cannot contain duplicate quadruples .
Example :
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
The set of quads satisfying the requirements is :
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
It's more than three .
This time it's still i For the first number ,i+1 For the second number j, then j+1 For the third number left,length-1 For the fourth number right.
i Traverse for the first layer , Notice here i For the range of [0,nums.length-4].
Still sort first ,j The benchmark is i+1, And j The range is [i+1,nums.length-3].
The core double pointer remains unchanged .
i and j Pay attention to the comparison with the previous one in the cycle , If the same, skip , but j Unable to join i Compare the elements of . Because this is just a position comparison ,i and j Certainly not .
Last but not least left and left+1 Than . Move right
right and right-1 Than , Move left
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
// At least four numbers
if (nums.length > 3) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
// Ascending , If the smallest ones are larger than the target value , What are you looking for
if (nums[i] > target && nums[i] >= 0) {
break;
}
// i Benchmarking
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 Benchmark for the second element
if (j > i + 1 && nums[j] == nums[j - 1]) {
System.out.println(" The second element repeats the previous one , Skip this cycle ");
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(" And for " + sum);
if (sum == target) {
System.out.println(" Equal to the target value ");
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(" Less than the target , Left pointer plus ");
} else if ((sum > target)) {
right--;
System.out.println(" Greater than the target value , Right pointer subtraction ");
}
}
System.out.println(i + " " + j);
}
System.out.println(i);
}
}
return result;
}

边栏推荐
- mysql存储过程
- mongoDB的三种基础备份方法
- Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
- [serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
- 慕课11、微服务的用户认证与授权
- 两个文件 合并为第三个文件 。
- Tiktok practice ~ sharing module ~ generate short video QR code
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
- 大家都能看得懂的源码(一)ahooks 整体架构篇
- C primer plus学习笔记 —— 3、字符的IO(输入/输出)
猜你喜欢

回溯思路详解

leetcode刷题:字符串01(反转字符串)

Uni app uses canvas to draw QR code

Unity——Mathf. Similarities and differences between atan and atan2

Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)

leetcode刷题:字符串02( 反转字符串II)

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

动态规划111

leetcode刷题:字符串03(剑指 Offer 05. 替换空格)

分布式ID生成系统
随机推荐
windows系统下怎么安装mysql8.0数据库?(图文教程)
Can I open an account online? Is it safe?
Super VRT
Invocation failed Unexpected end of file from server
Detailed explanation of shutter textfield
WebView load pdf
0 basic C language (0)
leetcode刷题:字符串01(反转字符串)
Uni app uses canvas to draw QR code
SentinelResource注解详解
Tiktok practice ~ search page ~ scan QR code
On the origin of the dispute between the tradition and the future of database -- AWS series column
股票开户的具体步骤是什么?网上开户安全吗?
定长内存池
c语言简单的登录
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
浏览器事件循环
Stringutils judge whether the string is empty
How to install mysql8.0 database under Windows system? (Graphic tutorial)
Dynamic parameter association using postman