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, 你需要在数组里找 ...
随机推荐
- HTML学习笔记(下)
表格标签 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...
- Qt5.2.1交叉编译,带tslib插件
一: 源码下载地址: 1.1: 平台: 主机:ubuntu 14.04 开发板: cpu arm-cortex-a8,故而我在配置我的qmake.conf的时候填写的为armV7-a QT版本: qt ...
- Django---view视图FBV&CBV
一:创建项目和应用: 或者用命令创建: 1:django-admin.py startproject CBV&FBV 2: cd CBV&FBV (路径切到该文件夹下) 3: pyth ...
- PHP 获取真实IP地址
function getClientIp($type = 0) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return ...
- 20145230《java学习笔记》第十周学习总结
20145230<Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或 ...
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
原文链接:http://www.cnblogs.com/zouzf/p/3984628.html C++和C#互调时经常会带一些参数过去例如最常见的字符串,如果字符串里有中文的话,会发现传递过去后变成 ...
- poj 1011 :Sticks (dfs+剪枝)
题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...
- c# 判断一个ip通不通 能不能ping通
方法一: 已经证实能用的. using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...
- 反射+type类+Assembly+特性
什么是元数据,什么是反射: 程序是用来处理数据的,文本和特性都是数据,而我们程序本身(类的定义和BLC中的类)这些也是数据. 有关程序及其类型的数据被称为元数据(metadata),它们保存在程序的程 ...
- SQL 存在一个表而不在另一个表中的数据
原文链接:http://blog.csdn.net/windren06/article/details/8188136 (转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w ...