LeetCode K-diff Pairs in an Array
原题链接在这里:https://leetcode.com/problems/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].
题解:
HashMap<Integer, Integer> hm 计数 num与出现次数.
再iterate一遍hm, 看是否key+k也在hm中.
Note: corner case 例如 k<0.
Time Complexity: O(n), n = nums.length. Space: O(n).
AC Java:
public class Solution {
public int findPairs(int[] nums, int k) {
if(nums == null || nums.length == 0 || k < 0){
return 0;
} int res = 0;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int num : nums){
hm.put(num, hm.getOrDefault(num, 0)+1);
} for(Map.Entry<Integer, Integer> entry : hm.entrySet()){
if(k == 0){
if(entry.getValue() > 1){
res++;
}
}else{
if(hm.containsKey(entry.getKey()+k)){
res++;
}
}
}
return res;
}
}
类似Two Sum.
LeetCode K-diff Pairs in an Array的更多相关文章
- 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 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] 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 ...
- [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解题报告(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 ...
- 【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 ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
随机推荐
- 光标定位 + commonAncestor
self.cmd.range.setStartBefore().collapse(true) self.cmd.select() 通过dom节点设置range的范围 <h1>conte ...
- UI控件之UINavigationController
ViewController1 *vc1=[[ViewController1 alloc]init]; UINavigationController *nav1=[[UINavigationContr ...
- 【TopCoder】SRM151 DIV2 练习总结
第一次做完整的SRM题,刷完感觉萌萌哒,不过自己对java中很多细节不熟练,还要边做题边google. 250分的题:判断字符串序列是否是前缀码,如果不是,返回第一个违反前缀码规则的字符串. 简单的暴 ...
- php数组函数-array_keys()
array_keys()函数返回包含数组中所有键名的一个新数组 如果提供了第二个参数,则返回键值为该值得键名 如果strict参数指定为true,则php会使用全等(===)来严格检查键值的 数据类型 ...
- windows简单使用etcd
一.下载安装选择版本 https://github.com/coreos/etcd/releases 二.解压 三.首先开启etcd 1.进入在etcd解压的目录中 2.etcd.exe 没有erro ...
- windows蓝屏错误小全
作者:siyizhu 日期:2005-11-27 字体大小: 小 中 大 引用内容 0 0x00000000 作业完成. 1 0x00000001 不正确的函数. 2 0x00000002 系统 ...
- java深入探究12-框架之Structs
注意每次修改配置文件后必须项目重启 Structs2=structs1+xwork Struct2框架预先实现了一些功能: 1.请求数据的封装:2.文件上传的功能3.对国际化功能的简化4.文件效验功能 ...
- C# 反射通过GetCustomAttributes方法,获得自定义特性
http://blog.csdn.net/litao2/article/details/17633107 使用反射访问: 自定义属性的信息和对其进行操作的方法. 一.实例1 1.代码: 如:Syste ...
- 有若干个箱子,假设每个箱子的最大承重为 MaxW 。将货物分配装箱
今天在博客园中看到一个博问,就写了下实现代码. 问题: 有若干个箱子,假设每个箱子的最大承重为 MaxW .有一批物品,它们的重量分别为w1.w2...Wn,假设每个物品的重量都不超过箱子承重.写个算 ...
- 在openrc中从keystone V2 到 V3
#!/bin/bash ] then echo "Usage $0 <keystone.rc>" exit fi . $ NEW_OS_AUTH_URL=`echo $ ...