【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日
随机推荐
- Comet OJ - Contest #1
A:随便怎么暴力. #include<bits/stdc++.h> using namespace std; #define ll long long #define N 25 char ...
- PHP Yii2 composer环境安装
PHP Yii2 composer环境安装 composer 安装 任意目录执行: php -r "copy('https://install.phpcomposer.com/install ...
- android wake lock 电源管理简单学习
需要配置清单文件:<uses-permission android:name="android.permission.WAKE_LOCK" /> 也可以参考我之前写的这 ...
- linux服务器上,yum、rpm、源码编译安装及卸载
源码的编译安装及卸载 源码安装三部曲 1.生成makefile编译文件./configure 一般安装包下面都有一个configure文件,用来生成makefile编译文件常用的参数: --prefi ...
- css:a:visited限制
:active 对于:active伪类可以在div上生效.没有限制 :visited使用限制 :visited只适用于带href的a标签.如果给a标签绑定了click事件,那跳转的url必须跟href ...
- python+appium模拟手机物理按键操作
一句代码:driver.keyevent() 括号里填入的是手机物理按键的数字代号 driver.press_keycode() 括号里填入的是键盘按键的数字代号 手机物理 ...
- 可变字符串类 StringBuilder
string类创建的字符串是不可变的(同一内存中),每更改一次,就会新开辟内存,不利于高效频繁操作. 当频繁操作同一字符串变量时,建议使用StringBuilder. 可变字符串类StringBuil ...
- 【优秀的图片后期编辑工具】Luminar 3.1 for Mac
[简介] 今天和大家分享最新的 Luminar for Mac 3.1 版本,支持中文界面,Luminar是一款Mac上优秀的图片后期处理工具,功能类似 Photoshop Lightroom 等软 ...
- Aras 发布Web Services
https://blog.csdn.net/plm888/article/details/10890173
- Linux基础系统优化及常用命令
# Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...