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,让找出有多少对不重复的数对 (i, j) 使得i和j的差刚好为k。由于k有可能为0,而只有含有至少两个相同的数字才能形成数对,那么就是说需要统计数组中每个数字的个数。可以建立每个数字和其出现次数之间的映射,然后遍历 HashMap 中的数字,如果k为0且该数字出现的次数大于1,则结果 res 自增1;如果k不为0,且用当前数字加上k后得到的新数字也在数组中存在,则结果 res 自增1,参见代码如下:

解法一:

class Solution {
public:
int findPairs(vector<int>& nums, int k) {
int res = , n = nums.size();
unordered_map<int, int> m;
for (int num : nums) ++m[num];
for (auto a : m) {
if (k == && a.second > ) ++res;
if (k > && m.count(a.first + k)) ++res;
}
return res;
}
};

下面这种方法没有使用 HashMap,而是使用了双指针,需要给数组排序,节省了空间的同时牺牲了时间。遍历排序后的数组,然后在当前数字之前找最后一个和当前数之差大于k的数字,若这个数字和当前数字不是同一个位置,且和上一轮 pre 不是同一个数字,且和当前数字之差正好为k,那么结果 res 自增1,然后将 pre 赋值为这个数字,参见代码如下:

解法二:

class Solution {
public:
int findPairs(vector<int>& nums, int k) {
int res = , n = nums.size(), j = , pre = INT_MAX;
sort(nums.begin(), nums.end());
for (int i = ; i < n; ++i){
while (j < i && nums[i] - nums[j] > k) ++j;
if (j != i && pre != nums[j] && nums[i] - nums[j] == k) {
++res;
pre = nums[j];
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/532

类似题目:

Minimum Absolute Difference in BST

参考资料:

https://leetcode.com/problems/k-diff-pairs-in-an-array/submissions/

https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/100104/two-pointer-approach

https://leetcode.com/problems/k-diff-pairs-in-an-array/discuss/100098/Java-O(n)-solution-one-Hashmap-easy-to-understand

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] K-diff Pairs in an Array 数组中差为K的数对的更多相关文章

  1. 532 K-diff Pairs in an Array 数组中差为K的数对

    详见:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ C++: class Solution { public: ...

  2. 215 Kth Largest Element in an Array 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...

  3. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  4. Leetcode532.K-diff Pairs in an Array数组中的K-diff数对

    给定一个整数数组和一个整数 k, 你需要在数组里找到不同的 k-diff 数对.这里将 k-diff 数对定义为一个整数对 (i, j), 其中 i 和 j 都是数组中的数字,且两数之差的绝对值是 k ...

  5. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  6. LeetCode 215. 数组中的第K个最大元素(Kth Largest Element in an Array)

    题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...

  7. [Swift]LeetCode215. 数组中的第K个最大元素 | Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. LeetCode题解 | 215. 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

  9. [LeetCode]215. 数组中的第K个最大元素(堆)

    题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出 ...

随机推荐

  1. Ubuntu16.0.4的磁盘管理

    ubuntu下硬盘无损分区移动修改工具 原创 2014年04月13日 :: ubuntu上面其实有很好的分区调整工具,gparted,非常好使用 安装非常简单 sudo apt-get install ...

  2. SpagoBi开发示例——员工离职人数统计

    1.开发工具:SpagoBIStudio_5.1,操作界面和使用方法和eclipse没差 安装参考:http://www.cnblogs.com/starlet/p/4778334.html   2. ...

  3. alpha-咸鱼冲刺day3

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 今天把数据库的表给建好了,学长那边把登陆跟注册页面也做好了(纯页面,html5+css的那种) 四,问题困难 日常啥都不会,百度 ...

  4. 20145237 实验五《Java网络编程》

    20145237 实验五<Java网络编程> 一.实验内容 •1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: •2.利用加解密代码包,编译运行代码,一人加密,一人解密: •3 ...

  5. OSI七层协议模型、TCP/IP四层模型学习笔记

    1. OSI七层和TCP/IP四层的关系 1.1 OSI引入了服务.接口.协议.分层的概念,TCP/IP借鉴了OSI的这些概念建立TCP/IP模型. 1.2 OSI先有模型,后有协议,先有标准,后进行 ...

  6. XFTP连接主机文件名显示中文乱码且不能下载的解决方法

    Xftp连接主机文件名显示中文乱码且不能下载的本地解决方法 原因:Xftp编码格式问题 解决方法:把Xftp的编码格式增加UTF-8 具体步骤:打开Xftp,文件-属性,在打开的属性界面中打开&quo ...

  7. 微信支付 chooseWXPay:fail

    本来以为解决了微信支付get_brand_wcpay_request:faill这个问题后就万事大吉了,结果又迈入了另一个坑... 问题原因: 1.生成签名的时间戳参数名timestamp的s大小写问 ...

  8. 点开GitHub之后,瑟瑟发抖...的我

    我说句实在话啊,GitHub这个网址真的很能勾起人学习的欲望,一进入GitHub的注册页面真的让我这个英语学渣瑟瑟发抖,瞬间立下个flag:好好学习英语..... 我对python的求知欲怎么能被英语 ...

  9. 爬虫模块BeautifulSoup

    中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html# 1.1      安装BeautifulSoup模块 ...

  10. web api 如何通过接收文件流的方式,接收客户端及前端上传的文件

    服务端接收文件流代码: public async Task<HttpResponseMessage> ReceiveFileByStream() { var stream = HttpCo ...