原题链接在这里: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. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...

  2. lelel-5

    一.样式有几种引入方式?link和@import有什么区别? 样式有3种引入方式: 外部样式(外联式Linking):是将网页链接到外部样式表<link rel="stylesheet ...

  3. 《机器学习实战第7章:利用AdaBoost元算法提高分类性能》

    import numpy as np import matplotlib.pyplot as plt def loadSimpData(): dataMat = np.matrix([[1., 2.1 ...

  4. 【计算机网络】OSI模型,TCPIP模型

    今天给大家分享的是两种模型的主要区别,夜视比较容易混淆的地方.我尽力用图形的方式来说问题,这样比较好理解~ (PS:画图真的不会,正在认真学,希望多多包含:)) 一.二者的模型对比 (这个图有点丑.. ...

  5. C# 处理base64 以及base64的原理分析

    base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxiaohui/ar ...

  6. Ubuntu下用crontab 部署定时任务

    用php做了一个网站,其中一个统计工能,需要每周定时用行.想看看有什么方法,之前看别人的东西,一般有2中方式,一个是php自带的定时任务,一个是用系统 带的,linux下的crontab和window ...

  7. CSS3的transform属性

    CSS3的一些属性可能比较新,有一些书从国外翻译到国内的时间上会延缓1-2年.所以有一些东西还需要及时整理. 下面说一下CSS3的一个属性:transform 其实字面上的意思已经很明显了就是变化,变 ...

  8. Spring初学之注解方式配置bean

    直接看代码: UserController.java package spring.beans.annotation.controller; import org.springframework.be ...

  9. 谷歌地图OGC WMTS服务规则

    http://mt0.google.cn/vt/lyrs=s&x=0&y=0&z=1 其中:z即为瓦片的层次,0层覆盖全球:y为行,从上往下为0~2^z-1:x为列,从左往右依 ...

  10. oracle创建存储过程中遇到的问题

    create or replace PROCEDURE CLEAR AS tname varchar(200);BEGIN tname:='''immediate trace name flush_c ...