【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
题目描述
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].
题目大意
找出一个数组中有多少对数,使得这对数差的绝对值等于k。相同的一对数字只计算一次。
解题方法
字典
遇到数组中某数的和或者差在不在数组中都是用字典
去算啊!这个题使用字典和set就能求出有多少个差为k的了,set能保证不重复计算相同的元素。
import collections
class Solution(object):
def findPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
answer = 0
counter = collections.Counter(nums)
for num in set(nums):
if k > 0 and num + k in counter:
answer += 1
if k == 0 and counter[num] > 1:
answer += 1
return answer
二刷的时候,同样地使用字典,只不过是先对k进行了一个判断,这样当k是正数的时候,直接用set就解决了。所以这个速度打败了100%的提交。
class Solution(object):
def findPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
res = 0
if k < 0: return 0
elif k == 0:
count = collections.Counter(nums)
for n, v in count.items():
if v >= 2:
res += 1
return res
else:
nums = set(nums)
for num in nums:
if num + k in nums:
res += 1
return res
C++版本的代码如下:
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
unordered_map<int, int> m;
for (int num : nums) {
m[num]++;
}
int res = 0;
for (const auto &it : m) {
if (k == 0 && it.second >= 2) {
res ++;
} else if (k > 0 && m.count(it.first + k)) {
res ++;
}
}
return res;
}
};
日期
2018 年 2 月 4 日
2018 年 11 月 27 日 —— 最近的雾霾太可怕
【LeetCode】532. K-diff Pairs in an Array 解题报告(Python & C++)的更多相关文章
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】1065. Index Pairs of a String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 852 Peak Index in a Mountain Array 解题报告
题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...
- LeetCode 961 N-Repeated Element in Size 2N Array 解题报告
题目要求 In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is re ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
随机推荐
- Prometheus概述
Prometheus是什么 首先, Prometheus 是一款时序(time series) 数据库, 但他的功能却并非支部与 TSDB , 而是一款设计用于进行目标 (Target) 监控的关键组 ...
- R2CNN模型——用于文本目标检测的模型
引言 R2CNN全称Rotational Region CNN,是一个针对斜框文本检测的CNN模型,原型是Faster R-CNN,paper中的模型主要针对文本检测,调整后也可用于航拍图像的检测中去 ...
- vue-baidu-map相关随笔
一,使用vue-baidu-map 1.下载相关包依赖 npm i vue-baidu-map 2.在main.js中import引入相关包依赖,在main.js中添加如下代码: import B ...
- 一个神奇的JS混淆,JSFuck!
JSFuck,整体由6个字符[, ], (, ), !, +组成,但却是可以正常运行的JS代码,JSFuck程序可以在任何Web浏览器或引擎中运行解释JavaScript! 看一段代码,源代码为:do ...
- 网卡命令ifconfig
• ifconfig • service network restart • ifdown eth0 • ifdown eth0 #linux下run networkexport USER=lizhe ...
- android studio 使用 aidl(一)基础用法
最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方 ...
- Linux学习 - 正则表达式
一.正则表达式与通配符 正则表达式:在文件中匹配符合条件的字符串,正则是包含匹配 通配符:用来匹配符合条件的文件名,通配符是完全匹配 二.基础正则表达式 元字符 作用 a* a有0个或任意多个 . 除 ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- ubantu上编辑windows程序
命令简记 cd $GOROOT/src cp -r $GOROOT /root/go1.4 CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash 操作 ...
- supervise安装与使用
确认当前是否已经安装which supervise/usr/local/bin/supervise 软件下载安装-------------------------------------------- ...