原题链接在这里:https://leetcode.com/problems/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].

题解:

HashMap<Integer, Integer> hm 计数 num与出现次数.

再iterate一遍hm, 看是否key+k也在hm中.

Note: corner case 例如 k<0.

Time Complexity: O(n), n = nums.length. Space: O(n).

AC Java:

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

类似Two Sum.

LeetCode 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. [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 ...

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

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

  6. leetcode解题报告(13):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 i ...

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

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

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

  9. C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...

随机推荐

  1. focus + select

    focus使光标定位到目标节点之后 select选中光标所在位置的全部内容

  2. Yii2 使用 Beanstalk 队列系统

    参考网址: Beanstalk:https://github.com/kr/beanstalkd Beanstalk console:https://github.com/ptrofimov/bean ...

  3. iOS Xcode 8 快捷键 (注释 失效 处理)

    在升级后,好用的VVDocumment 插件不能用了.(但是苹果这次内置了好多好用的插件,也有自己的注释功能了 AddDocumentation) 上网上有查到 传播很广泛的一条信息 "这个 ...

  4. UNIX 系统常用管理命令

    一. 引言 UNIX系统作为一种强大的多用户分时操作系统,在越来越多的场合受到了应用,同时,对UNIX的系统管理的要求也随之越来越多,但目前的书籍对UNIX系统管理命令介绍的并不是很多.本文主要是针对 ...

  5. 跨平台移动开发_Windows 8平台使用 PhoneGap 方法

    原文地址: Using PhoneGap in Windows 8 Store Applications 下载phonegap 2.9.1 下载地址: https://codeload.github. ...

  6. 黑色CSS3立体动画菜单

    在线演示 本地下载

  7. tomcat 正常启动但不能访问

    Eclipse中的Tomcat可以正常启动,不过发布项目之后,无法访问,包括http://localhost:8080/的小猫页面也无法访问到,报404错误.这是因为Eclipse所指定的Server ...

  8. 外推主要发布平台(JM)

    百家.搜狐.一点.头条   主要发布平台: 搜狐(权重高.收录好.审核相对宽松) https://mp.sohu.com/mpfe/v3/login 网易号(开通网易号,会有网易博客,网易博客可被收录 ...

  9. java定时任务Quartz Demo(2.X)

    直接上代码 public class HelloQuartz implements Job{ @Override public void execute(JobExecutionContext Jec ...

  10. python 操作mongoDB数据库

    网上关于python 操作mongoDB的相关文章相对不是很多,并且质量也不是很高!下面给出一个完整的 增删改查示例程序! #!/usr/bin/python # -*- coding: utf-8 ...