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

边栏推荐
- Database SQL statement writing
- 西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
- JS mobile terminal touch screen event
- [most detailed] the latest and complete redis interview (70)
- 0基础学c语言(2)
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- C language simple login
- ImageView, glide load long picture (glide load picture)
- [serial] shuotou O & M monitoring system 01 overview of monitoring system
- Is it safe to open an account for CICC Wealth Online?
猜你喜欢
Detailed explanation of stored procedures in MySQL

Development of NFT for digital collection platform

Dynamic planning 111

Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading

Tiktok practice ~ search page ~ video details

Dynamic parameter association using postman

MySQL - database creation and management

How to install mysql8.0 database under Windows system? (Graphic tutorial)

mongoDB的三种基础备份方法

Unity——Mathf. Similarities and differences between atan and atan2
随机推荐
2022/02/14 line generation
【连载】说透运维监控系统01-监控系统概述
回首望月
Jz-062- the k-th node of binary search tree
Muke 11. User authentication and authorization of microservices
swagger:如何生成漂亮的静态文档说明页
0基础学c语言(3)
Serial port application program based on gd32
C exercise. Class list plus records, display records and clear records
网上开户万一免五到底安不安全?
Detailed explanation of retrospective thinking
Dynamic parameter association using postman
Unity——Mathf. Similarities and differences between atan and atan2
【最详细】最新最全Redis面试大全(70道)
Establish a connection with MySQL
leetcode刷题:字符串01(反转字符串)
30. 串联所有单词的子串
0基础学c语言(1)
QT两种方法实现定时器
浏览器事件循环