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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. ios-真机调试出错信息

    更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found                   在Xcode中当你在更新了你得证书 ...

  2. UdpClient类客户端和服务端demo

    服务端demo static IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 0); static UdpClient udp = new UdpClie ...

  3. 连接zookeeper集群

    连接到ZooKeeper集合 ZooKeeper类通过其构造函数提供connect功能.构造函数的签名如下 : ZooKeeper(String connectionString, int sessi ...

  4. js splice()方法

    splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 实例 例子 1 在本例中,我们将创建一个新数组,并向其添加一个元素: <script ty ...

  5. RegexHelper

    ylbtech-Unitity-cs: RegexHelper 验证帮助类 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1,RegexMail #region RegexMail pu ...

  6. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.5.安装oracle11gr2 database 软件与创建数据库

    3.5.安装oracle11gr2 database 软件与创建数据库 3.5.1.安装Oracle 11gr2 Database 以oracle 用户登录到节点一,切换到软件安装目录,执行安装. 在 ...

  7. Linux的PCI驱动分析

    1. 关键数据结构 PCI设备上有三种地址空间:PCI的I/O空间.PCI的存储空间和PCI的配置空间.CPU可以访问PCI设备上的所有地址空间,其中I/O空间和存储空间提供给设备驱动程序使用,而配置 ...

  8. 爬虫扒下 bilibili 视频信息

    B站算是对爬虫非常非常友好的网站啦! 修改转载已取得腾讯云授权 在以上两篇文章中我们已经在腾讯云服务器上搭建好了 Python 爬虫环境了,下一步就是在云服务器上爬上我们的爬虫,抓取我们想要的数据: ...

  9. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  10. 《暗黑世界V1.6》服务器代码执行图

    <暗黑世界V1.6>服务器代码执行图 (原文地址:http://www.9miao.com/forum.php?mod=viewthread&tid=44016&highl ...