当前位置:网站首页>LeetCode 532. K-diff number pairs in array
LeetCode 532. K-diff number pairs in array
2022-07-03 09:01:00 【Sasakihaise_】
【 Hashtable 】 Compare the dishes made by yourself , First put all the points into the hash table , Then enumerate each element , Find out whether the larger element is in the hash table .k=0 This situation needs to be considered separately .
class Solution {
public int findPairs(int[] nums, int k) {
int ans = 0;
if(k == 0){
Map<Integer, Integer> map = new HashMap();
for(var x: nums){
map.put(x, map.getOrDefault(x, 0) + 1);
}
for(var key: map.keySet()){
if(map.get(key) > 1) ans++;
}
return ans;
}
Set<Integer> set = new HashSet(){
{
for(var x: nums) add(x);
}};
for(var x: set){
if(set.contains(x + k)){
ans++;
}
}
return ans;
}
}
【 Improved hash table 】 because k Is constant , So for any tuple , We only need to save a smaller value , The specific method is :
1. Open two hash tables , An element used to store enumerated elements
2. The other is used to store smaller values that meet the conditions
class Solution {
// 2:40 hash
public int findPairs(int[] nums, int k) {
Set<Integer> visit = new HashSet();
Set<Integer> ans = new HashSet();
for(var x: nums){
if(visit.contains(x - k)){
ans.add(x - k);
}
if(visit.contains(x + k)){
ans.add(x);
}
visit.add(x);
}
return ans.size();
}
}
【 Sort + Two points 】 Sort the array first , Then for each element , Find out whether it is larger than this element starting from the next coordinate k Value . It should be noted that the calculated elements should be skipped , Because it's in order , Repeating elements are all together , So judge whether it is the same as the previous element .
class Solution {
// Sort + Two points 2:50 7
public int binarySearch(int[] nums, int target, int left){
int right = nums.length - 1;
while(left <= right){
int mid = (left + right) >>> 1;
if(nums[mid] == target) return mid;
else if(nums[mid] > target) right = mid - 1;
else left = mid + 1;
}
return -1;
}
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
int ans = 0;
for(var i = 0; i < nums.length; i++){
if(i != 0 && nums[i] == nums[i - 1]) continue;
if(binarySearch(nums, nums[i] + k, i + 1) != -1) ans++;
}
return ans;
}
}
【 Sort + Double pointer 】 We found the sorted array , Some element nums[i] And elements bigger than him nums[j],j With i Moving backwards . In this way, it has the characteristics of double pointers .
class Solution {
// Sort + Double pointer 3:07
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
int ans = 0, j = 1;
for(var i = 0; i < nums.length; i++){
if(i != 0 && nums[i] == nums[i - 1]) continue;
while(j < nums.length && nums[j] <= nums[i] + k) j++;
if(j - 1 != i && nums[j - 1] == nums[i] + k) ans++;
}
return ans;
}
}
边栏推荐
- Dom4j遍历和更新XML
- Using variables in sed command
- LeetCode 535. TinyURL 的加密与解密
- Convert video to GIF
- 樹形DP AcWing 285. 沒有上司的舞會
- DOM 渲染系统(render mount patch)响应式系统
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- 20220630学习打卡
- [rust notes] 06 package and module
- Get the link behind? Parameter value after question mark
猜你喜欢
Debug debugging - Visual Studio 2022
Facial expression recognition based on pytorch convolution -- graduation project
Sending and receiving of request parameters
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
DOM 渲染系统(render mount patch)响应式系统
Divide candy (circular queue)
TP5 multi condition sorting
Phpstudy 80 port occupied W10 system
LeetCode 532. 数组中的 k-diff 数对
MySQL three logs
随机推荐
LeetCode 508. 出现次数最多的子树元素和
Try to reprint an article about CSDN reprint
22-05-26 西安 面试题(01)准备
JS ternary operator - learning notes (with cases)
状态压缩DP AcWing 91. 最短Hamilton路径
数位统计DP AcWing 338. 计数问题
Find the combination number acwing 885 Find the combination number I
Monotonic stack -84 The largest rectangle in the histogram
What is the difference between sudo apt install and sudo apt -get install?
Analysis of Alibaba canal principle
Alibaba canal actual combat
[concurrent programming] thread foundation and sharing between threads
Deep parsing (picture and text) JVM garbage collector (II)
Solution of 300ms delay of mobile phone
The method of replacing the newline character '\n' of a file with a space in the shell
Facial expression recognition based on pytorch convolution -- graduation project
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Parameters of convolutional neural network
Summary of methods for counting the number of file lines in shell scripts
Markdown learning