【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日
随机推荐
- ExcelPower_Helper插件下载与更新日志
ExcelPower_Helper插件下载.功能简述与演示 ExcelPower_Helper最新版本为:0.4.5,截止到目前为止. 下载地址: 链接:https://pan.baidu.com/s ...
- 【转】Linux之crontab定时任务命令
1. crontab命令概念 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使 ...
- postgresql数据库去重方法
数据库去重有很多方法,下面列出目前理解与使用的方法 第一种 通过group by分组,然后将分组后的数据写入临时表然后再写入另外的表,对于没有出现再group by后面的field可以用函数max,m ...
- 如何获得select被选中option的value和text
如何获得select被选中option的value和text 一:JavaScript原生的方法 1:拿到select对象: var myselect=document.getElementById( ...
- UOJ#449. 【集训队作业2018】喂鸽子(期望dp)
题意 有 \(n\) 只鸽子,每只鸽子需要 \(k\) 粒玉米才能喂饱.问每次随意喂给 \(n\) 个鸽子中的一个,期望多久所有鸽子都被喂饱. 对于 \(998244353\) 取模. 数据范围 \( ...
- 20165223《网络对抗技术》Exp4 恶意代码分析
目录 -- 恶意代码分析 恶意代码分析说明 实验任务目标 实验内容概述 schtasks命令使用 实验内容 系统运行监控 恶意软件分析 静态分析 virscan分析和VirusTotal分析 PEiD ...
- pwn-ROP(2)
通过int80系统只对静态编译有效,动态编译需要用其他方法 本题提供了一个地址输入端,输入函数地址会返回该函数的实际地址,我们用得到的实际地址-偏移地址=基地址,然后用基地址+任意函数的偏移地址就可以 ...
- 深度学习中优化【Normalization】
深度学习中优化操作: dropout l1, l2正则化 momentum normalization 1.为什么Normalization? 深度神经网络模型的训练为什么会很困难?其中一个重 ...
- spring的事件驱动模型
在工作中会遇到这样的业务,生成一个订单后需要给指定的用户发送短信或者邮件,但是短信或者邮件发送失败又不会影响正常的业务: 这里介绍通过ApplicationContext和spring的@EventL ...
- Linux工具安装和常用配置
1 常用开发工具安装 1 安装Mysql ①基本安装 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm: s ...