当前位置:网站首页>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;
}
}边栏推荐
- LeetCode 324. 摆动排序 II
- [concurrent programming] working mechanism and type of thread pool
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Method of intercepting string in shell
- PHP uses foreach to get a value in a two-dimensional associative array (with instances)
- [rust notes] 06 package and module
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- XPath实现XML文档的查询
- Arbre DP acwing 285. Un bal sans patron.
- 20220630学习打卡
猜你喜欢
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call

数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”

On the setting of global variable position in C language
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

剑指 Offer II 029. 排序的循环链表

22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

Find the combination number acwing 886 Find the combination number II

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了

LeetCode 532. 数组中的 k-diff 数对
随机推荐
AcWing 785. 快速排序(模板)
Gif remove blank frame frame number adjustment
Facial expression recognition based on pytorch convolution -- graduation project
Apache startup failed phpstudy Apache startup failed
Monotonic stack -503 Next bigger Element II
Find the combination number acwing 886 Find the combination number II
Tree DP acwing 285 A dance without a boss
The method of replacing the newline character '\n' of a file with a space in the shell
请求参数的发送和接收
[rust notes] 12 closure
DOM 渲染系统(render mount patch)响应式系统
Six dimensional space (C language)
ES6 promise learning notes
Divide candy (circular queue)
createjs easeljs
Memory search acwing 901 skiing
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
LeetCode 515. 在每个树行中找最大值
[concurrent programming] concurrent tool class of thread
php public private protected