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 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].
题目标签:Array, Two Pointers
题目给了我们一个nums array 和一个 k, 让我们找出有几对 相差k 的配对。
建立一个HashMap 把nums 的数字num 作为key 存入;把数字num 出现的 次数当作value 存入。
遍历map 的keys, 找 key + k 在map 里存不存在,存在的话说明这一对是k-diff pair。这种情况是当k 不等于 0;
当k = 0, 就要用到map 里的value 了,当key 出现的次数大于1 的时候, 至少2,那么 key 和 key 也是一对k-diff pair。
Java Solution:
Runtime beats 26.84%
完成日期:05/10/2017
关键词:Array, HashMap
关键点:把num当作key,把出现次数当作value 存入map,再遍历keys 来找k-diff pair
public class Solution
{
public int findPairs(int[] nums, int k)
{
int res = 0;
HashMap<Integer, Integer> map = new HashMap<>(); // set up the HashMap, key: number; value: occurrence.
for(int i=0; i<nums.length; i++)
{
// if map doesn't have it, add it into map and set value to 1.
// if map has it, increase it value by 1.
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); } // iterate the map
for(Integer key: map.keySet())
{
if(k == 0 && map.get(key) > 1) // if k == 0 , only check it's value
res++;
else if(k > 0 && map.containsKey(key + k)) // if k > 0, check the map has (key + k) as a key
res++; } return res;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/6545075.html
LeetCode 题目列表 - LeetCode Questions List
LeetCode 532. K-diff Pairs in an Array (在数组中相差k的配对)的更多相关文章
- leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项
https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...
- 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- 数组中的k个最小值
问题:输入n个整数,找出其中最小的k个数. 方案一:将输入的n个整数进行排序,输出前k个数即为所求的k个最小数.时间复杂度为O(nlogn). 方案二:创建一个大小为k的容器,来存储最小的k个数.遍历 ...
- 寻找数组中第K大数
1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- 前端算法题:找出数组中第k大的数字出现多少次
题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...
随机推荐
- Linux第二篇【系统环境、常用命令、SSH连接、安装开发环境】
系统环境 我们知道Windows的出色就在于它的图形界面那一块,而Linux对图形界面的支持并不是那么友好-其实我们在Windows下对图形界面进行的操作都是得装换成命令的方式的! 当然了,我们在Ub ...
- python实例编写(7)---测试报告与测试套件(多个py文件,1个py文件内多个用例)
一. 一个.py文件批量执行测试用例(一个.py文件下多个用例执行) 如果直接使用:unittest.main(),则按字母顺序执行, 对于前后之间又依赖关系的用例,需要按特定的顺序执行,则使用 s ...
- c#中的格式输出
Reference:http://blog.csdn.net/fightfaith/article/details/48137235
- C# 各种常用集合类型的线程安全版本
在C#里面我们常用各种集合,数组,List,Dictionary,Stack等,然而这些集合都是非线程安全的,当多线程同时读写这些集合的时候,有可能造成里面的数据混乱,为此微软从Net4.0开始专门提 ...
- TCP/IP笔记
TCP/IP 连接 三次握手 TCP/IP 四次分手 @TODO TIME_WAIT 状态 有三种状态可以进入此状态 1.由FIN-WAIT-2,双方不同时发起FIN,主动关闭的一方在完成自身发起的关 ...
- thinkphp的select和find的区别
hinkphp是比较好的php开发框架,能比较快速的开发MVC架构的管理系统,我们需要用到 select()和find()方法,两个方法都能返回数据集数组,但有什么不同呢?先看一下我的代码对比:$te ...
- Redis学习——Redis持久化之RDB备份方式保存数据
从这一个介绍里面知道,redis比memcache作为缓存数据库强大的地方,一个是支持的数据类型比较多,另一个就是redis持久化功能. 下面就介绍Redis的持久化之RDB! 一:什么是redis的 ...
- UI自动化测试(二)浏览器操作及对元素的定位方法(xpath定位和css定位详解)
Selenium下的Webdriver工具支持FireFox(geckodriver). IE(InternetExplorerDriver).Chrome(ChromeDriver). Opera( ...
- POJ1422Air Raid(二分图,最小不相交路径覆盖)
Air Raid Consider a town where all the streets are one-way and each street leads from one intersecti ...
- 学习如何看懂SQL Server执行计划——基本知识篇
一.基本概念 1.数据的读取 页(page)是SQL SERVER可以读写的最小I/O单位.即使只需访问一行,也要把整个页加载到缓存之中,再从缓存中读取数据.物理读取是从磁盘上读取,逻辑读取是从缓存中 ...