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 ...
随机推荐
- TZOJ1294吃糖果
#include<stdio.h> int main() { ],mi,i,max,s; scanf("%d",&t); while(t--) { scanf( ...
- Codeforces 1245 D. Shichikuji and Power Grid
传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...
- Session共享问题---理论
随着网站访问量增加,初期的一台服务器已经完全不能支持业务,这个时候我们就需要增加服务器设备,来抗住请求的增量,如下所示: 负载均衡的目的本来就是要为了平均分配请求,所以没有固定第一次访问和第二次访问的 ...
- Kafka 消息中间件
kafka简介与应用场景 Apache Kafka是分布式发布-订阅消息系统,在 kafka官网上对 kafka 的定义:一个分布式发布-订阅消息传递系统. 它最初由LinkedIn公司开发,Link ...
- C# 对象互转
/// <summary> /// 适用于初始化新实体 /// </summary> static public T RotationMapping<T, S>(S ...
- C#获取ip
string name = Dns.GetHostName(); string ip = Dns.GetHostAddresses(name).First().ToString();
- 设置pictureBox的边框颜色(转载)
原文地址:https://www.cnblogs.com/hardsoftware/p/5720545.html private void pictureBox2_Paint(object sende ...
- 【原创】大叔经验分享(86)hive和mysql数据互导
hive和mysql数据互导,首先想到的是sqoop,并且可以和调度框架(比如oozie等)配合配置定时任务,还有一种更简单的方式是通过spark-sql: CREATE OR REPLACE TEM ...
- C++ raw string literal
raw string literal 以 R"( 开头, )" 结束,是可以跨越多行的字符串字面值,转义字符如 \t\n 在raw string literal中是普通的文本 ...
- # 机器学习算法总结-第九天(XGboost)