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,从数组中找出差值为k元素的个数。

public int findPairs(int[] nums, int k) {
if(nums == null || nums.length ==0 || k < 0) return 0;
HashMap<Integer,Integer> map = new HashMap<>();
int count = 0;
for(int i:nums)
{
map.put(i, map.getOrDefault(i, 0) + 1);
}
for(Map.Entry<Integer, Integer> entry : map.entrySet())
{
if(k == 0)
{
if(entry.getValue() >= 2)
count ++;
}
else {
if(map.containsKey(entry.getKey() +k))
count ++;
}
}
return count;
}

代码的思想很简单 ,先使用map计算每个值出现的次数,假设nums=[3, 1, 4, 1, 5] k=2,那么map的结果是[1=2, 3=1, 4=1, 5=1]

  • 1.如果k=0,找出出现次数大于2元素的个数
  • 2.如果k != 0,这个时候巧妙的使用set,找出是否存在entry.getKey() +k的值。

    本题目的要点是map和set的使用。

532. K-diff Pairs in an Array的更多相关文章

  1. LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  2. 532. K-diff Pairs in an Array绝对值差为k的数组对

    [抄题]: Given an array of integers and an integer k, you need to find the number of unique k-diff pair ...

  3. [LeetCode] K-diff Pairs in an Array 数组中差为K的数对

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  4. 【leetcode_easy】532. K-diff Pairs in an Array

    problem 532. K-diff Pairs in an Array 题意:统计有重复无序数组中差值为K的数对个数. solution1: 使用映射关系: 统计数组中每个数字的个数.我们可以建立 ...

  5. [LeetCode] K Inverse Pairs Array K个翻转对数组

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  6. [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  7. 629. K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  8. 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  9. [Swift]LeetCode532. 数组中的K-diff数对 | 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 t ...

随机推荐

  1. 【JAVA零基础入门系列】Day2 Java集成开发环境IDEA

    开发环境搭建好之后,还需要一个集成开发环境也就是IDE来进行编程.这里推荐的IDE是IDEA,那个老掉牙的Eclipse还是先放一边吧,(手动滑稽). IDEA的下载地址:http://www.jet ...

  2. C++PrimerPlus第6版 第四章——复合类型

    1,复合类型主要包含:数组.结构.联合.枚举.类.指针.引用等. 2,数组.长度必须确定.即编译阶段,数组的长度就得确定好.所以只能使用常量(#define.const)声明数组长度.如果使用变量声明 ...

  3. Vim的基本使用(一)

    本文为原创文章,转载请标明出处 目录 1.移动光标 2.屏幕滚动 3.模式查找 4.位置标记 5.删除文本 6.撤销与重做 7.插入文本 8.复制与移动 9.修改文本 10.写入与退出 1. 移动光标 ...

  4. python读取命令行参数的方法

    1.sys模块 需要模块:sys参数个数:len(sys.argv)脚本名:    sys.argv[0]参数1:     sys.argv[1]参数2:     sys.argv[2] test.p ...

  5. [js高手之路]匀速运动与实例实战(侧边栏,淡入淡出)

    javascript中,如何让一个元素(比如div)运动起来呢? 设置基本的样式,一定要让div有定位( 当然用margin的变化也可以让元素产生运动效果 ); <style> div { ...

  6. github部分有意思的库记录

    1.MMDrawerController (抽屉框架) https://github.com/mutualmobile/MMDrawerController 2.ijkplayer视频直播框架 htt ...

  7. 多线程(RunLoop)

    1.RunLoop的概念及作用 2.RunLoop的使用 3.RunLoop的相关类 4.RunLoop的工作原理 5.小结 6.思考 什么是RunLoop? 从字面意思上是一直循环跑,事实上就是一个 ...

  8. win10 uwp 上传Nuget 让别人用我们的库

    Nuget 我们的开发经常使用别人的dll,那么我们需要每次都从网上下载,然后复制到我们的项目, 而不知道我们的dll是否安全? 当我们的库更新的时候,我们又需要从网上搜索,这样不好,于是我们就用Nu ...

  9. Java基础总结--多线程总结1

    ----进程和线程-----1.概述:简单理解一个进程就是一个正在运行的程序(程序在内存中的所属空间)程序只有在运行的时候才会被加载进内存2.进程内部的划分进程不会直接执行,只是被当作分配内存资源的基 ...

  10. cordova封装h5为app,cookie不可用解决方法

    //创建cookie function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdat ...