当前位置:网站首页>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;
}
}边栏推荐
- 我們有個共同的名字,XX工
- 精彩回顾|I/O Extended 2022 活动干货分享
- Apache startup failed phpstudy Apache startup failed
- Facial expression recognition based on pytorch convolution -- graduation project
- Format - C language project sub file
- 即时通讯IM,是时代进步的逆流?看看JNPF怎么说
- XPath实现XML文档的查询
- Parameters of convolutional neural network
- Using variables in sed command
- Character pyramid
猜你喜欢

Try to reprint an article about CSDN reprint

20220630学习打卡

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

Gif remove blank frame frame number adjustment

Redux - learning notes

Allocation exception Servlet

XPath实现XML文档的查询

LeetCode 324. 摆动排序 II

22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism

Monotonic stack -84 The largest rectangle in the histogram
随机推荐
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
JS ternary operator - learning notes (with cases)
TP5 order multi condition sort
Find the combination number acwing 886 Find the combination number II
LeetCode 871. 最低加油次数
Es8 async and await learning notes
[concurrent programming] working mechanism and type of thread pool
DOM render mount patch responsive system
求组合数 AcWing 886. 求组合数 II
拯救剧荒,程序员最爱看的高分美剧TOP10
LeetCode 30. 串联所有单词的子串
Divide candy (circular queue)
Deep parsing (picture and text) JVM garbage collector (II)
Methods of using arrays as function parameters in shell
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
网络安全必会的基础知识
分配异常的servlet
[rust note] 10 operator overloading
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?