【easy】532. K-diff Pairs in an Array
这道题给了我们一个含有重复数字的无序数组,还有一个整数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的更多相关文章
- 【easy】215. Kth Largest Element in an Array 第K大的数
class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【BZOJ3436】小K的农场(差分约束)
[BZOJ3436]小K的农场(差分约束) 题面 由于BZOJ巨慢无比,使用洛谷美滋滋 题解 傻逼差分约束题, 您要是不知道什么是差分约束 您就可以按下\(Ctrl+W\)了 #include< ...
- 【BZOJ3110】【LG3332】[ZJOI2013]K大数查询
[BZOJ3110][LG3332][ZJOI2013]K大数查询 题面 洛谷 BZOJ 题解 和普通的整体分治差不多 用线段树维护一下每个查询区间内大于每次二分的值\(mid\)的值即可 然后再按套 ...
- 【美】范·K·萨普曼 - 通向财务自由之路(2013年11月26日)
<通向财务自由之路> 作 者:[美]范·K·萨普曼 译 者:董梅 系 列: 出 版:机械工业出版社 字 数:约40千字 阅读完成:2013年11月26日
随机推荐
- Linux程序前台后台切换
1.在Linux终端运行命令的时候,在命令末尾加上 & 符号,就可以让程序在后台运行 root@Ubuntu$ ./tcpserv01 & 2.如果程序正在前台运行,可以使用 Ctrl ...
- 技术的极限(6): 密码朋克精神(Cypherpunk Spirit)
上一篇:技术的极限(5): 识别计算与技术背后的心智 下一篇:技术的极限(7): 处理复杂与分层 目录: ** 0x01 数据和App分离 ** 0x02 蒸汽朋克/赛博朋克/密码朋克 ** 0x03 ...
- ajax属性详解
https://blog.csdn.net/mooncom/article/details/52402836 资料库: $.ajaxSetup()方法为将来的ajax请求设置默认值. http://w ...
- MySQL中怎么对varchar类型排序问题(转)
在mysql默认order by 只对数字与日期类型可以排序,但对于varchar字符型类型排序好像没有用了,下面我来给各位同学介绍varchar类型排序问题如何解决. 今天在对国家电话号码表进行 ...
- vue实现懒加载
- DAY23、面向对象特性
一.复习1.类: 对象属性的查找顺序:先找自身再找类 类的名称空间:直接写在类中 对象的名称空间:写在__init__方法中,通过self.属性形成名称空间中的名字 类的方法:在类中用@classme ...
- Delphi 获取DataSet传入参数后的SQL命令
ClientDataSet1.CommandText := sSQL; ClientDataSet1.Params.Clear; ClientDataSet1.CommandText :='SEL ...
- Jetson TX1 compile pytorch issues
1. c++: internal compiler error: Killed (program cc1plus) reason: memory out, need swapfile 2. NCCL ...
- 《AutoCAD Civil 3D .NET二次开发》勘误1
第十三章atc文件中Displayname应为DisplayName,注意Name的N为大写,否则参数名称无法正常显示. 给您带来的不便深表歉意!
- drf相关问题
drf自定义用户认证: 登录默认 使用django的ModelBackend,对用户名和密码进行验证.但我们平时登录网站时除了用户名也可以用邮箱或手机进行登录,这就需要我们自己扩展backend 一. ...