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. Tomcat部署时war和war exploded的区别

    转自徐刘根的Tomcat部署时war和war exploded区别以及平时踩得坑 一.war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: ...

  2. 获取XIB子视图的两个方法

    创建了一个XIB文件 CommentCell.xib,并设置好UIImageView的tag为100,昵称UILabel的tag为101,时间的UILabel的tag为102,并制定cell为Comm ...

  3. wpf datagrid performance

    http://stackoverflow.com/questions/1704512/wpf-toolkit-datagrid-scrolling-performance-problems-why h ...

  4. CentOS6 安装golang

    CentOS6 安装golang 下载 wget http://golangtc.com/static/go/1.8/go1.8.linux-amd64.tar.gz 1 1 1 解压 tar -xz ...

  5. YUV12(420) (from)to RGB24

    直接上代码 #include <opencv2/opencv.hpp> #include <stdio.h> #define min(a,b) ((a<b)?a:b) # ...

  6. 在Linux中切换用户时变成-bash-4.3$

    增加普通用户 [root@git-node1 ~]#useradd nulige [root@git-node1 ~]#passwd nulige 输入两次密码 [root@git-node1 ~]# ...

  7. Win7如何查看自己得Win7版本号

    如何查看Windows 7详细系统版本号? --Windows 7系统知识100问之七十一 责任编辑:姜惠田作者:IT168 老姜   2009-08-05 前言:微软新一代操作系统Windows 7 ...

  8. FBX导入错误 :ImportFBX Errors:

    原地址:http://www.cnblogs.com/lopezycj/archive/2012/05/16/unity3d_tuchao.html Unity3D吐槽1--FBX导入 Unity3d ...

  9. acm之路--母函数 by小宇

    母函数又叫生成函数,原是数学上的一个名词,是组合数学中的一个重要理论. 生成函数是说,构造这么一个多项式函数g(x).使得x的n次方系数为f(n). 对于母函数,看到最多的是这样两句话: 1.&quo ...

  10. [WebView学习之三]:使用WebView来创建Apps

    上一篇我们学习了([WebView学习之二]:使用Web Apps 支持不同分辨率屏),今天我们来继续学习. (博客地址:http://blog.csdn.net/developer_jiangqq) ...