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

/*
思路:因为存在k=0的情况,并且答案需要是完全不重复的数对,所以
可以先存下每个数字出现的次数,如果k=0且每个数字出现次数大于1,则count++
如果k>0,且当前数字+k之后的数字也出现过,那么count++
*/
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
int count = ;
int len = nums.size();
unordered_map<int,int> map;
for (auto num:nums) map[num]++;
for (auto a:map){
if (k== && a.second>) count++;
if (k> && map.count(a.first + k)) count++;
}
return count;
}
};

***一定要学会unordered_map,auto等,这个很好写哦

【easy】532. K-diff Pairs in an Array的更多相关文章

  1. 【easy】215. Kth Largest Element in an Array 第K大的数

    class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int ...

  2. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  3. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  4. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  5. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  6. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  7. 【BZOJ3436】小K的农场(差分约束)

    [BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include< ...

  8. 【BZOJ3110】【LG3332】[ZJOI2013]K大数查询

    [BZOJ3110][LG3332][ZJOI2013]K大数查询 题面 洛谷 BZOJ 题解 和普通的整体分治差不多 用线段树维护一下每个查询区间内大于每次二分的值\(mid\)的值即可 然后再按套 ...

  9. 【美】范·K·萨普曼 - 通向财务自由之路(2013年11月26日)

    <通向财务自由之路> 作 者:[美]范·K·萨普曼  译 者:董梅 系 列: 出 版:机械工业出版社 字 数:约40千字 阅读完成:2013年11月26日

随机推荐

  1. MR-join连接1......

    MR-join连接

  2. Python链表的实现与使用(单向链表与双向链表)

    参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...

  3. linux第一天

    第一天内容:1>. 命令解析器2>. Linux快捷键3>. Linux 系统目录结构4>. 用户目录5>. 文件和目录操作6>. 文件和目录的属性7>. 文 ...

  4. MongoDB之常用操作

    最近经常使用MongoDB来进行数据的操作,特此记录总结一下

  5. ##Django中Application labels aren't unique解决方法##

    pip更新了所有插件,发现了按平常编码遇到些问题,记录下. Django错误 django.core.exceptions.ImproperlyConfigured: Application labe ...

  6. babel 插件编写

    一.开始 工具链接: 每一个节点都有如下所示的接口(Interface): interface Node { type: string; } 字符串形式的 type 字段表示节点的类型(如: &quo ...

  7. 动态chart Demo

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. vue打包静态资源路径不正确的解决办法【转】

    vue项目完成打包上线的时候很多人都会碰到静态资源找不到的问题,常见的有两个 1.js,css路径不对 解决办法:打开config/index.js,将其中的assetsPublicPath值改为’. ...

  9. django-haystack全文检索

    一:使用的工具haystack是django的开源搜索框架,该框架支持Solr,Elasticsearch,Whoosh, *Xapian*搜索引擎,不用更改代码,直接切换引擎,减少代码量.搜索引擎使 ...

  10. Windows 上连接本地 Linux虚拟机上的 mysql 数据库

    查看本机ip ifconfig  查看当前的 3306 端口状态 netstat -an|grep 3306 当前是外部无法连接状态 修改访问权限 默认的 mysql 是只能本机连接, 因此需要修改配 ...