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 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].
分析
一开始我是想用两个for循环做的,后来发现漏了各种边界情况,提交数次都无法ac,深感无力。后来0看了讨论区(https://discuss.leetcode.com/topic/81702/o-n-concise-solution-c )的解法恍然大悟:用一个unordered_map即可解决!
用unordered_map<int,int>,key 值保存数组元素,value保存该元素个数。
因此只需遍历这个map,判断这个map里是否存在键值加上k后的键值即可。
唯一需要考虑的是k=0的情况,因为k=0时只有两个相等的元素才能满足。并且根据题意,即使有n对匹配元素(val1,val2)是k匹配的,也只算做一对。因此k=0时,只需要判断这个元素的个数即可,有两个及以上便将个数加1.
代码如下:
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
if(k < 0)return 0;
int k_diff = 0;
unordered_map<int,int>um;
for(int i = 0; i != nums.size(); ++i)
++um[nums[i]];
if(k != 0){
for(auto it = um.begin(); it != um.end(); ++it)
if(um.find(it->first + k) != um.end())
++k_diff;
}else{
for(auto it = um.begin(); it != um.end(); ++it)
if(it->second > 1)
++k_diff;
}
return k_diff;
}
};
心得
写个心得。
unordered_map真心好用啊,很多操作的复杂度也低,在大多数情况下简直是上上之选,以后解题的时候可以优先考虑用unordered_map!
leetcode解题报告(13):K-diff Pairs in an Array的更多相关文章
- LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- [LeetCode解题报告] 502. IPO
题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- [LeetCode解题报告] 703. 数据流中的第K大元素
题目描述 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包 ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
随机推荐
- vue-cookies的使用
安装vue-cookies npm install vue-cookies --save 使用vue-cookies // 在main.js中 // require var Vue = require ...
- MarkdownPad2安装与破解-转载
MarkdownPad安装包下载链接链接:https://pan.baidu.com/s/1o7c4W7C2d8zCPh5z7y4IvQ提取码:e4bf 下载解压之后,找要MarkdownPad2.e ...
- hdu 1698 线段数的区间更新 以及延迟更新
先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; ...
- C# 延迟初始化 Lazy<T>
概念:延时初始化重点是延时,用时加载,意思是对象在使用的时候创建而不是在实例化的的时候才创建. 延时加载主要应用的场景: 数据层(ADO.NET或Entity Framework等ORM,Java ...
- vue-filters(过滤器)
局部过滤器: <html> <head> <title>vue</title> <meta charset="utf-8"&g ...
- element-ui 日期插件让结束日期大于开始日期
<el-date-picker v-model="addForm.startDate" type="date" size="mini" ...
- 安装theano踩过的坑(gpu)
参考 http://deeplearning.net/software/theano/install.html TensorFlow出了点问题 python3.7的环境 pip安装 keras已经安装 ...
- .Net IOC 框架
CastleWindsor 参见:CastleWindsor | .Net IOC 框架 AutoFace 参见:AutoFace | .Net IOC 框架 Unity 参见:Unity | .Ne ...
- [Docker][Hadoop]基于Docker1.12.3 搭建Hadoop 2.7.2 集群以及简单分析
一 Hadoop简介 Hadoop 2.7.2 Doc refer to http://hadoop.apache.org/docs/r2.7.2/ HDFS (The following is a ...
- 机器学习中的数学-强大的矩阵奇异值分解(SVD)及其应用
版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...