原题链接在这里: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. node+npm安裝配置

    控制臺輸入node 根據提示安裝   sudo apt-get install -g npm配置淘寶源 npm config set registry https://registry.npm.tao ...

  2. 前端之JQuery [续]

    JQuery使用技巧 1.prop属性实现全选,反选,取消功能 需求: 实现全选,反选,取消功能 代码如下: <!DOCTYPE html> <html lang="en& ...

  3. linux 定时任务未执行php脚本

    1,对于无法执行php文件,首先你应该考虑的问题是是否php代码有错误,你可以先检查一下你的php代码,或者可以在linux上面执行一下这个文件,看是否能够执行成功:如果成功了,就说明是crontab ...

  4. 建议13:使用Python模块re实现解析小工具

    # -*- coding:utf-8 -*- # ''' Python re 的主要功能: re.compile(pattern[, flags]) 把正则表达式的模式和标识转化成正则表达式对象,供 ...

  5. HTML table元素

    搬运,内容来自HTML Dog. 简单示例 <!DOCTYPE html> <html> <body> <table> <tr> <t ...

  6. C++逗号表达式

    c++中,逗号表达式的结果是最右边表达式的值

  7. ubuntu中如何添加IP

    编辑网卡配置文件vi /etc/network/interfaces 在配置文件下增加新的IP配置 之后重启网络/etc/init.d/networking restart

  8. SecureCRT按退格键出现^H问题

    1.  选择选项>>会话选项>>终端>>映射键

  9. [转载]ORA-00313:无法打开日志组1(线程 1)的成员_ORA-00312:

    原文地址:1)的成员_ORA-00312:">ORA-00313:无法打开日志组1(线程 1)的成员_ORA-00312:作者:Sweet_薇薇毅 今天用系统清理工具把系统垃圾清理了一 ...

  10. MapReduce-输入分片与记录

    一个输入分片(split)就是一个由单个map操作来处理的输入块.每一个map操作只处理一个输入分片.每个分片被划分为若干个记录,每条记录就是一个键值对,map一个接一个地处理记录.输入分片和记录都是 ...