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

边栏推荐
- 超分之VRT
- Two methods of QT to realize timer
- [Bayesian classification 2] naive Bayesian classifier
- Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
- Daily basic use of alicloud personal image warehouse
- The source code that everyone can understand (I) the overall architecture of ahooks
- Is it safe to open an online account in case of five-year exemption?
- 515. 在每个树行中找最大值
- Stop being a giant baby
- Tiktok practice ~ sharing module ~ copy short video link
猜你喜欢

这些地区考研太疯狂!哪个地区报考人数最多?

Uni app uses canvas to draw QR code

GEE:计算image区域内像素最大最小值

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

c语言99乘法表

分布式ID生成系统

The relationship between the development of cloud computing technology and chip processor

leetcode刷题:哈希表08 (四数之和)
随机推荐
超分之VRT
2022/02/14 line generation
Unity - URP get camera stack
C primer plus learning notes - 3. Character IO (input / output)
Gd32 USB composite device file descriptor
QT两种方法实现定时器
windows系统下怎么安装mysql8.0数据库?(图文教程)
ImageView, glide load long picture (glide load picture)
Database SQL statement writing
[most detailed] the latest and complete redis interview (70)
两个文件 合并为第三个文件 。
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
【最详细】最新最全Redis面试大全(42道)
不要做巨嬰了
Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
C language simple login
Detailed explanation of stored procedures in MySQL
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
C# 练习。类列表加记录,显示记录和清空记录
The relationship between the development of cloud computing technology and chip processor