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].

题目标签:Array, Two Pointers

  题目给了我们一个nums array 和一个 k, 让我们找出有几对 相差k 的配对。

  建立一个HashMap 把nums 的数字num 作为key 存入;把数字num 出现的 次数当作value 存入。

  遍历map 的keys, 找 key + k 在map 里存不存在,存在的话说明这一对是k-diff pair。这种情况是当k 不等于 0;

           当k = 0, 就要用到map 里的value 了,当key 出现的次数大于1 的时候, 至少2,那么 key 和 key 也是一对k-diff pair。

Java Solution:

Runtime beats 26.84%

完成日期:05/10/2017

关键词:Array, HashMap

关键点:把num当作key,把出现次数当作value 存入map,再遍历keys 来找k-diff pair

 public class Solution
{
public int findPairs(int[] nums, int k)
{
int res = 0;
HashMap<Integer, Integer> map = new HashMap<>(); // set up the HashMap, key: number; value: occurrence.
for(int i=0; i<nums.length; i++)
{
// if map doesn't have it, add it into map and set value to 1.
// if map has it, increase it value by 1.
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); } // iterate the map
for(Integer key: map.keySet())
{
if(k == 0 && map.get(key) > 1) // if k == 0 , only check it's value
res++;
else if(k > 0 && map.containsKey(key + k)) // if k > 0, check the map has (key + k) as a key
res++; } return res;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/6545075.html

LeetCode 题目列表 - LeetCode Questions List

LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)的更多相关文章

  1. leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项

    https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...

  2. 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...

  3. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  4. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  6. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  7. 数组中的k个最小值

    问题:输入n个整数,找出其中最小的k个数. 方案一:将输入的n个整数进行排序,输出前k个数即为所求的k个最小数.时间复杂度为O(nlogn). 方案二:创建一个大小为k的容器,来存储最小的k个数.遍历 ...

  8. 寻找数组中第K大数

    1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  9. 前端算法题:找出数组中第k大的数字出现多少次

    题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...

随机推荐

  1. CentOS克隆机器步骤,图文教程

    在上一篇文章中,主要介绍了CentOS的装机步骤,这一篇文章主要介绍如何从一台装好的CentOS克隆出另一台机器,并且配置好网络. 首先被克隆的CentOS需要关机, 右键点击被克隆的CentOS-& ...

  2. 【Spring】Spring的bean装配

    前言 bean是Spring最基础最核心的部分,Spring简化代码主要是依赖于bean,下面学习Spring中如何装配bean. 装配bean Spring在装配bean时非常灵活,其提供了三种方式 ...

  3. Day-1:初识开发板与基础知识

    买的这款51,ARM,AVR三合一的单片机,也不知道后面具体使用会不会有问题,先玩玩看吧. ------------------------------------------------------ ...

  4. HTML5基本标签的使用

    第一次写这种东西,肯定存在许多不足之处,还望大家多多担待,我会继续加油的!我也是一名HTML5的初学者,只是将这几周在课堂上所听到的东西分享给大家. 下面给大家介绍一下H5! 一.<!DOCTY ...

  5. angular-bootstrap ui-date组件问题总结

    使用angular框架的时候,之前用的时间控件是引入My97DatePicker组件实现的,但是因为 1.My97DatePicker样式不太好看以及偶尔会出现底部被遮盖的情况.点击不可编辑input ...

  6. angularui 分页

    分页组件的使用 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> ...

  7. Spring Boot Document Part II(下)

    Part II. Getting started 11. 开发第一个Spirng Boot Application使用Spring Boot的关键特征开发一个基于JAVA Web的“Hello Wor ...

  8. Java子线程中的异常处理(通用)

    在普通的单线程程序中,捕获异常只需要通过try ... catch ... finally ...代码块就可以了.那么,在并发情况下,比如在父线程中启动了子线程,如何正确捕获子线程中的异常,从而进行相 ...

  9. 记2017问鼎杯预赛的wp---来自一个小菜鸡的感想

    这次准备写一下几个misc和密码题目..很坑. 打了一整天的比赛,越来越觉得自己很菜了. 有一道题目叫做"真真假假",这道题目只有一个提示--Xor.第一眼知道是异或,也就知道这一 ...

  10. 移动端效果之Picker

    写在前面 接着前面的移动端效果的研究,这次来看看picker选择器的实现原理 移动端效果之Swiper 代码看这里:github 1. 核心解析 1.1 基本HTML结构 <!-- 说明: 1. ...