【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日
随机推荐
- Markdown语法基础
Markdown基本语法 创建 2018-09-07 by YANHAI 标题:Setext方式 三个或更多 大标题 === 小标题 --- 大标题 小标题 标题:Atx方式 # 内容 (一级标题) ...
- AOP - 1 基本概念
1.AOP (面向切面编程) AOP,Aspect Oriented Programming,意为:面向切面编程, 通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术, AOP是OOP的 ...
- linux 上 mysql 的使用
1.登录mysql 第一次登录 没有密码 可以直接输入 mysql 有密码可以使用 mysql -u root -p 回车会提示需要输入密码 -u 用户名 -p 密码 这个mysql文件在/us ...
- Python 学习笔记 - 不断更新!
Python 学习笔记 太久不写python,已经忘记以前学习的时候遇到了那些坑坑洼洼的地方了,开个帖子来记录一下,以供日后查阅. 摘要:一些报错:为啥Python没有自增 ++ 和自减 --: 0x ...
- 本机Jenkins的使用
1.启动jenkins: 命令:java -jar D:\toolspackage\jenkins\jenkins.war 打开jenkins网页:http://localhost:8080/ 2. ...
- Python 目录指引
1.0 Python 基础整合 1.1 变量 1.2 数据类型 1.3 基础语法 1.4 文件操作 1.5 函数 1.6 生成器 1.7 迭代器 1.8 装饰器 1.9 字符集 2.0 Python ...
- Wiki leaks files backup
Wiki leaks files backup 来源 http://ftp.icm.edu.pl/packages/incoming/torrent/ Index of /packages/inco ...
- mysql varcahr转int类型
cast(yysid as SIGNED INTEGER)
- ubuntu下使用ss-qt5
第一步:.安装ss-qt5 通过PPA源安装,仅支持Ubuntu 14.04或更高版本. 打开终端 sudo add-apt-repository ppa:hzwhuang/ss-qt5 sudo a ...
- django rest framework pagination
REST framework 包含对可定制分页样式的支持.这使你可以将较大的结果集分成单独的数据页面. 分页 API 支持: 以分页链接的形式作为响应内容的一部分. 以分页链接的形式包含在响应的 he ...