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 ...
随机推荐
- 浅谈RBF函数
所谓径向基函数 (Radial Basis Function 简称 RBF), 就是某种沿径向对称的标量函数. 通常定义为空间中任一点x到某一中心xc之间欧氏距离的单调函数 , 可记作 k(||x-x ...
- msgfmt: command not found
sudo apt-get install gettext
- 【mybatis】mybatis中update更新原来的值加1
示例代码: floor的值 = floor原来的值+要更新的差距值 <update id="updateFloor" parameterType="com.pise ...
- 用Storyboard构建标签栏多页面应用程序UI
注: 貌似CSDN的显示效果不佳,假设有须要的话我能够上传pdf格式的: 另外假设文章中有错误还请给位多多提意见,谢谢. pdf格式文档:http://download.csdn.net/detail ...
- Qt 之 模仿 QQ登陆界面——样式篇
一.简述 今天晚上花了半天时间从QQ登录界面抠了些图,顺便加了点样式基本上实现了QQ的登陆界面全部效果.虽不说100%相似,那也有99.99%相似了哈O(∩_∩)O. QQ好像从去年开始,登录界面有了 ...
- linux下GPRS模块的应用程序
---------------------------------------------------------------------------------------------------- ...
- 鸟哥的linux私房菜服务器架设篇第五章linux常用网络指令
ifconfig主要可以手动启动观察修改网络接口的相关参数 ifdown,ifup用来启动和关闭接口,后面直接接接口名称 两部主机两点沟通:ping 两主机之间各节点分析 traceroute 查看本 ...
- linux 输出重定向 何时会写文件
linux 输出重定向 何时会写文件 测试到了8K才会进行flush:
- 各版本JDK1.5-1.8新特性
概述 一jdk15新特性 泛型 foreach 自动拆箱装箱 枚举 静态导入Static import 元数据Metadata 线程池 Java Generics 二jdk16新特性 Desktop类 ...
- docker集群——Mesos集群下的负载均衡marathon-lb
前面的章节介绍了Mesos+Zookeeper+Marathon的Docker管理平台,接下来介绍如何在该平台下构建负载均衡. 默认情况下,mesos marathon会把app发布到随机节点的随机端 ...