LeetCode532. K-diff Pairs in an Array
Description
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).
my program
思路:本题主要需要理解题目中给出的k-diff这个新概念,明白了后就不难解出答案。
利用一个map储存数组中的数字,以及出现的次数。当k=0的时候,统计map中value大于1的key的数目,即为所求k-diff的个数;当k>0的时候查找key′=key+k在map中是否存在,若存在即k-diff对,count+1
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
int count = 0;
if (k < 0) return count;
map<int, int> mp;
for (auto number : nums) {
mp[number]++;
}
map<int, int>::iterator it;
if (k == 0) {
for (it = mp.begin(); it != mp.end(); it++) {
if (it->second > 1)
count++;
}
}
else {
for (it = mp.begin(); it != mp.end(); it++) {
if (mp.find(it->first+k) != mp.end())
count++;
}
}
return count;
}
};
Submission Details
72 / 72 test cases passed.
Status: Accepted
Runtime: 39 ms
LeetCode532. K-diff Pairs in an Array的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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-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 t ...
- 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 t ...
随机推荐
- Asp.Net MVC part6 WebAPI
两种web服务SOAP风格:基于方法,产品是WebServiceREST风格:基于资源,产品是WebAPI可以返回json.xml类型的数据对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型 ...
- 协同过滤中的Grey Sheep问题
寒神解释:某些用户的倾向性和品味没有一致性,比较散.因此在协同过滤这种算法里,没办法和某个group有很高的相似/一致度,推荐会失效. 我理解是寻找邻居时候计算得到的相似度和其他用户相似度都非常小,或 ...
- LiDAR Textbook & Automated Road Network Extraction
Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...
- 关于yum的一些安装问题
最近折腾CentOS和kubernetes,遇到一些安装问题,把和yum相关的逐步总结如下: 如何用本地的cdrom作为yum源 mount /dev/cdrom /mnt 先查询是否安装了creat ...
- react使用引入svg的icon;svg图形制作
由于手头的icon有限,需要使用更多的图标,就得找外援: 1.react安装icon插件,使用插件里已经有的图标 https://react-icons.netlify.com/#/ React Ic ...
- Spring的学习(IoC,AOP)等
下面这个系列是非常好的例子: http://www.yiibai.com/spring/spring-3-hello-world-example.html 正在看,把一些基础夯实. IoC可以从下面一 ...
- MooseFS分布式文件系统介绍及安装教程详解
MFS系统由4个部分构成:master.metalogger.chunkserver.client. 1.Master —— mfs的大脑,记录着管理信息,比如:文件大小,存储的位置,份数等,和inn ...
- 怎样编写高效android代码
基于Android相关设备作为嵌入式设备范畴,在书写App应用的时候要格外关注效率.而且受电池电量的限制.这就导致嵌入式设备有诸多考虑.有限处理能力.因此就要求我们尽量去写高效的代码. 本文讨论了非常 ...
- 【监控】使用 Grafana、collectd 和 InfluxDB 打造现代监控系统
参考资料:Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器:http://www.oschina.net/p/grafana 使用 Grafana.collectd 和 ...
- Genymotion 在win10 下的安装
首先我在Genymotion官网上并没有找到他的安装程序.据说是在注冊后,通过邮件里的链接下载,结果也没有看到.最后详细在哪下的,忘了收藏.我下的是 genymotion-2.5.3-vbox.exe ...