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. vue传数据到模态框中

    写一下我的做法 <a data-toggle="modal" data-target="#delete_tpl_modal" class="bt ...

  2. Zabbix(一) : 简介以及Server端安装

    一.什么是Zabbix? zabbix由AlexeiVladishev首先开发,目前在维护的是Zabbix SIA.ZABBIX是一个企业级的开源分布式监控解决方案. zabbix为监控网络和服务器的 ...

  3. Java历程-初学篇 Day02变量,数据类型和运算符

    一,数据类型 1,基础数据类型 整型 byte short int long 浮点型 float double 字符型 char 布尔类型 boolean 2,引用类型 String 字符串型 二,变 ...

  4. python之串口操作

    1.安装pyserial linux上直接安装: #python2 sudo pip install pyserial #或者python3 sudo pip3 install pyserial Wi ...

  5. Jquery购物车jsorder改进版,支持后台处理程序直接转换成DataTable处理

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 【网络爬虫入门02】HTTP客户端库Requests的基本原理与基础应用

    [网络爬虫入门02]HTTP客户端库Requests的基本原理与基础应用 广东职业技术学院  欧浩源 1.引言 实现网络爬虫的第一步就是要建立网络连接并向服务器或网页等网络资源发起请求.urllib是 ...

  7. python re模块findall()详解

    今天写代码,在写到郑泽的时候遇到了一个坑,这个坑是re模块下的findall()函数. 下面我将结合代码,记录一下 import re string="abcdefg acbdgef abc ...

  8. JS代码中!!的用法,以及代码性能对比

    一.!!的理解 解释: !!的意思就是把一个任意类型的值转换为布尔类型的值,一个!是取非 再一个!又取非,相当于把这个数据转换为boolen类型了. 使用场景: 常常用在if(a).if(!!a)这样 ...

  9. 查找算法(I) 顺序查找 二分查找 索引查找

    查找 本文为查找算法的第一部分内容,包括了基本概念,顺序查找.二分查找和索引查找.关于散列表和B树查找的内容,待有空更新吧. 基本概念 查找(search)又称检索,在计算机上对数据表进行查找,就是根 ...

  10. 自学 Python 3 最好的 入门 书籍 推荐(附 免费 在线阅读 下载链接)

    请大家根据自己的实际情况对号入座,挑选适合自己的 Python 入门书籍: 完全没有任何编程基础:01 号书 少量编程基础,不求全,只希望能以最快的速度入门:02 号书 少量编程基础,有一定的英文阅读 ...