[leetcode-532-K-diff Pairs in an Array]
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array.
Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.
Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Note:
The pairs (i, j) and (j, i) count as the same pair.
The length of the array won't exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].
思路:
用一个map记录所有关键字,这样还能同时去掉了重复元素,然后遍历map,统计满足条件个数
注意 absolute difference 绝对值>=0
int findPairs(vector<int>& nums, int k)
{
if (k < )return ;
int result = ;
//sort(nums.begin(),nums.end());//不需要排序
map<int, int>table;
map<int, int>::iterator it;
for (int i = ; i < nums.size();i++)
{
table[nums[i]]++;
}
for (it = table.begin(); it != table.end();it++)
{
if (k!= &&table.count(k + it->first))//比如此时first为1 而k=2 判断是否存在3
{
result++;
}
else if (k == && it->second >= )//说明存在至少两个元素值相等
{
result++;
}
}
return result;
}
[leetcode-532-K-diff Pairs in an Array]的更多相关文章
- LeetCode算法题-K-diff Pairs in an Array(Java实现)
这是悦乐书的第254次更新,第267篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第121题(顺位题号是532).给定一个整数数组和一个整数k,您需要找到数组中唯一的k- ...
- LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] K-diff Pairs in an Array 数组中差为K的数对
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] K Inverse Pairs Array K个翻转对数组
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 532. K-diff Pairs in an Array绝对值差为k的数组对
[抄题]: Given an array of integers and an integer k, you need to find the number of unique k-diff pair ...
- [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 629. K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- LeetCode K-diff Pairs in an Array
原题链接在这里:https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description 题目: Given an array of i ...
- leetcode解题报告(13):K-diff Pairs in an Array
描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs i ...
随机推荐
- [刷题]Codeforces 746G - New Roads
Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...
- React入门---开始前的准备(上)-2
开始前准备: 1.安装配置nodeJs(nodeJS官网) 检查安装成功: node -v npm -v 2.使用NPM配置React开发环境 (1). 创建项目文件夹(创建文件夹时,文件夹名不要起为 ...
- Java Date Classes
References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...
- CentOS-Zabbix-agent客户端的编译安装
系统环境: CentOS 6.7 官网下载安装包:http://www.zabbix.com/download 本文用的是Zabbix 3.0 LTS 上传至客户端服务器并解压 .tar.gz 进入解 ...
- SmartCoder每日站立会议02
1.站立会议内容 经过昨天一天的学习,大家了解到了很多新的知识,在各自任务实现方面也遇到了问题,比如首页的css样式实现.API接口的连接和地图接入的样式,经过今日站立会议,大家在一起讨论了各自出现的 ...
- Linux Namespaces机制
转自:http://www.cnblogs.com/lisperl/archive/2012/05/03/2480316.html Linux Namespaces机制提供一种资源隔离方案.PID,I ...
- wampServer 2.5 64位 更改"www 目录"不成功
已经指到自己新目录了,修改了 apache的httpd.conf里面的内容如下 修改: DocumentRoot "e:/phproot/" 修改: <Directory & ...
- [速成]了解一致性hash算法
定义 一致性hash算法,在维基百科的定义是: Consistent hashing is a special kind of hashing such that when a hash table ...
- poj3207
poj3207 题意 平面上,一个圆,圆的边上按顺时针放着n个点.现在要连m条边, 比如a,b,那么a到b可以从圆的内部连接,也可以从圆的外部连接. 给你的信息中,每个点最多只会连接的一条边.问能不能 ...
- Java之枚举
1.定义 enum 是一种数据类型,与 全局常量比较相似,都是全局的并且是可以通过类名调用的 与全局常量区别 枚举功能更强大,可以有属性和方法 枚举比全局常量更加的规范 2.枚举特性 1)可以有属性以 ...