532. 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].
题目的意思是给定一个值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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 【leetcode_easy】532. K-diff Pairs in an Array
problem 532. K-diff Pairs in an Array 题意:统计有重复无序数组中差值为K的数对个数. solution1: 使用映射关系: 统计数组中每个数字的个数.我们可以建立 ...
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- [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 ...
随机推荐
- Docker 最常用的监控方案 - 每天5分钟玩转 Docker 容器技术(78)
当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态将会变得越来越重要. 在本章中,我们将讨论几个目前比较常用的容器监控工具和方案,为大家构建自己的监控系统提供参考. 首先我们会讨 ...
- ZOJ-2091-Mean of Subsequence (反证法的运用!!)
http://blog.csdn.net/u014355480/article/details/40862041 ZOJ2091 题意:其实就是找后几个数的平均值的最大值!! (贪心策略!要找对) k ...
- javascript小节
javascript 语法总结 知识概要: (1)Javascript概述 1.1javascript是什么? 1.2JavaScript语言组成 1.3JavaScript与Html的结 ...
- 记录一次因为硬盘写满造成的redis无法连接
缘起: 今天早晨收到报警,服务不干活了,赶紧起来看问题... 为了尽快让服务可用,尝试重启服务,发现服务起不来,报错 redis connection failed! 看起来是redis挂了,但是发现 ...
- 深度学习入门篇--手把手教你用 TensorFlow 训练模型
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:付越 导语 Tensorflow在更新1.0版本之后多了很多新功能,其中放出了很多用tf框架写的深度网络结构(https://git ...
- Django安装Xadmin步骤
在Django中安装Xadmin替换原始的admin,下面介绍两种方法安装 第一种方法:pip安装 第一步: 直接pip安装xadmin pip install xadmin pip会同时安装上面三个 ...
- Servlet中路径信息总结
./ 当前目录 ../ 父级目录 / 根目录 资源寻找都是依靠路径,资源存储方式是按照哈希表运算的,所以路径的计算其实就是哈希值的计算. servlet中,所有路径的配置都要用绝对路径. 什么是绝对路 ...
- “华尔街之狼”:ICO是“史上最大骗局”
勘探船进村的那个夏季,父亲从城里带回了那把手电.手电的金属外壳镀了镍,看上去和摸起来一样冰凉.父亲进城以前采了两筐枸杞子,他用它们换回了那把锃亮的东西.父亲一个人哼着<十八摸>上路,鲜红透 ...
- apache+php+mysql运行环境
建议Apache2.4+php5.6+mysql5.5+phpmyadmin4.4.4 参考: http://jingyan.baidu.com/article/fcb5aff797ec41edaa4 ...
- LINUX 笔记-iostat命令
显示CPU和I/O统计信息 iostat的不带参数的显示CPU和I/ O的所有分区的统计信息 -c Display the CPU utilization report. -d Display the ...