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 ...
随机推荐
- iOS:自定义代码块{ }
1.Xcode本身带有编码常用的代码块可供使用,如下图 调用方法: (1)直接拖到代码区域中: (2)使用快捷键,键入 “while”, Xcode就会出现自动完成的提示 也可以自定义自己常用的代码块 ...
- 小二助手(react应用框架)-http访问
浏览地址http://118.25.217.253:4000 账号test密码123 qq讨论群:836719189 要写这个系统,就需要数据来源,让我们先来看看如果通过客户端调用服务端api拿到 ...
- Netty源码细节-accept、read(Linux os层 + Netty层代码细节)(转)
原文:http://budairenqin.iteye.com/blog/2215899 这篇分析一下accept的细节, 我觉得网络IO相关开发很多时候不能仅仅局限于java层, 尤其从accept ...
- org.hibernate.exception.ConstraintViolationException: could not delete:
转自:http://fireinwind.iteye.com/blog/848515 异常描述: org.hibernate.exception.ConstraintViolationExceptio ...
- android studio 警告 synchronization on non-final field
测试代码如下: public class SyncNonFinalField { private Object object = new Object(); public void start() { ...
- 在OpenErp的配置文件中为数据库密码加密
openerp连接数据库的用户名和密码可以命令行给出, 也可以设置在配置文件中, 如下例所示: db_user = openerp db_password = laoliu 因为它使用了明文的密码 ...
- VirtualBox实现宿主机和虚拟机之间网络的通讯
摘要:实现宿主机和虚拟机之间网络的通讯 环境: 宿主机操作系统 WindowsXP 虚拟机软件 VirtualBox 虚拟机操作系统 ...
- Linux下使用Fastboot给手机刷ROM
前言 一直在刷机.失败.刷机.失败中,还好今天有个任务能够使用fastboot刷机.好开心,最终不用切换系统了.(话说好久没有写代码了,身为一个互联网程序猿,不写代码我easy紧张). 开发环境 Ub ...
- Android AIDL Service 跨进程传递复杂数据
黑夜 黑夜给了我黑色的眼睛,我却用它寻找光明~ 传值方式 AIDL是同意跨进程传递值的,一般来说有三种方式: - 广播:这样的算是比較常见的一种方式了,传递小数据不错 - 文件:这个是保存到文件里.然 ...
- javascript 中对象的拷贝
http://blog.csdn.net/i10630226/article/details/52431562 在javascript中,对象都是值引用,也就是说,如果你常规的使用 "=&q ...