当前位置:网站首页>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;
}

边栏推荐
- 清华大学就光刻机发声,ASML立马加紧向中国出口光刻机
- 0 basic C language (3)
- 开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
- GEE:计算image区域内像素最大最小值
- 【最详细】最新最全Redis面试大全(42道)
- Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
- 云计算技术的发展与芯片处理器的关系
- BOM and DOM operations
- IDEA 报错:Process terminated【已解决】
- 孙老师版本JDBC(2022年6月12日21:34:25)
猜你喜欢

Dynamic parameter association using postman

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

云计算技术的发展与芯片处理器的关系

与 MySQL 建立连接

Muke 11. User authentication and authorization of microservices

回溯思路详解

Gee: calculate the maximum and minimum values of pixels in the image area
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

【连载】说透运维监控系统01-监控系统概述
MySQL中存储过程的详细详解
随机推荐
0基础学c语言(3)
C primer plus learning notes - 3. Character IO (input / output)
定长内存池
GEE:计算image区域内像素最大最小值
Détails de l'annotation des ressources sentinelles
460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
The source code that everyone can understand (I) the overall architecture of ahooks
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
Looking back at the moon
C: Reverse linked list
Establish a connection with MySQL
Detailed explanation of stored procedures in MySQL
[most detailed] latest and complete redis interview (42 tracks)
MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
孙老师版本JDBC(2022年6月12日21:34:25)
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
【贝叶斯分类3】半朴素贝叶斯分类器
0基础学c语言(2)
两个文件 合并为第三个文件 。
不要做巨嬰了