532 K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Example 1:

Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.

Example 2:

Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).

Example 3:

Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).

Note:

  1. The pairs (i, j) and (j, i) count as the same pair.
  2. The length of the array won't exceed 10,000.
  3. All the integers in the given input belong to the range: [-1e7, 1e7].

本题是寻找数组中差为k的数对的个数

 

方法一

复制代码
 1     public int findPairs(int[] nums, int k) {
 2        if (nums == null || nums.length == 0 || k < 0)   return 0;
 3         
 4         Map<Integer, Integer> map = new HashMap<>();
 5         int count = 0;
 6         for (int i : nums) {
 7             map.put(i, map.getOrDefault(i, 0) + 1);
 8         }
 9         
10         for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
11             if (k == 0) {
12                 //count how many elements in the array that appear more than twice.
13                 if (entry.getValue() >= 2) {
14                     count++;
15                 } 
16             } else {
17                 if (map.containsKey(entry.getKey() + k)) {
18                     count++;
19                 }
20             }
21         }
22         
23         return count;
24     }
复制代码

方法二

复制代码
 1     public  int findPairs1(int[] nums, int k) {
 2         if(k<0 || nums.length<=1){
 3             return 0;
 4         }
 5 
 6         Arrays.sort(nums);
 7         int count = 0;
 8         int left = 0;
 9         int right = 1;
10 
11         while(right<nums.length){
12             int firNum = nums[left];
13             int secNum = nums[right];
14             if(secNum-firNum<k)
15                 right++;
16             else if(secNum - firNum>k)
17                 left++;
18             else{
19                 count++;
20                 while(left<nums.length && nums[left]==firNum){
21                     left++;
22                 }
23                 while(right<nums.length && nums[right]==secNum){
24                     right++;
25                 }
26 
27             }
28             if(right==left){
29                 right++;
30             }
31         }
32         return count;
33     }
复制代码

 

posted @   daniel456  阅读(158)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示