[LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
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].
分三种情况讨论, 如果k< 0, return 0, 如果k == 0 , 看如果某个数至少有两个, +1, 如果k >0 , 利用Two sum的方法去找.
Code
class Solution(object):
def findPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
if k < 0:
return 0
if k==0:
return sum(val > 1 for val in collections.Counter(nums).values())
nums, d, ans = set(nums), set(), 0
for num in nums:
if num -k in d:
ans += 1
if num + k in d:
ans += 1
d.add(num)
return ans
[LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table的更多相关文章
- [LeetCode] 697. Degree of an Array_Easy tag: Hash Table
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- [LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] 1. Two Sum_Easy tag: Hash Table
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [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 ...
- [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 ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
随机推荐
- Android O 获取APK文件权限 Demo案例
1. 通过 aapt 工具查看 APK权限 C:\Users\zh>adb pull /system/priv-app/Settings . /system/priv-app/Settings/ ...
- filter对数组和对象的过滤
1,对数组的过滤 let arr = ['1', '2', '3'] let b = arr.filter(val => val === '2') console.log(b) // ['2] ...
- html5新增标签/删除标签
闲聊: 最近小颖工作稍微比较轻松,没事就看看慕课,看了看:HTML5之元素与标签结构,里面简单讲解了下HTML5的一些新特性,小颖之前没写过HTML5的页面,所以就当写笔记将那些新的特性整理出来,也方 ...
- Build step 'Execute Windows batch command' marked build as failure
坑爹的Jenkis,在执行windows命令编译.NET项目的时候命令执行成功了,但是却还是报了这样一个错: Build step 'Execute Windows batch command' ma ...
- 【CF827F】Dirty Arkady's Kitchen DP
[CF827F]Dirty Arkady's Kitchen 题意:给你一张n个点,m条边的有向图,每条边长度为1,第i条边在[li,ri)的时间内可以进入,求1到n的最短路. $n,m\le 5\t ...
- [Log]ASP.NET之HttpModule拦截404异常
Httpmodule代码: public class Error404Module : IHttpModule { public void Init(HttpApplication context) ...
- 安装pod
1.ruby升级最新 sudo gem update -n /usr/local/bin --system 2. $ gem sources *** CURRENT SOURCES *** https ...
- humid vs wet vs moist
想表达天气很潮湿该用哪个词呢? 跟一个美国人聊天,我说wet他没听清,然后我说moist,然后他反应过来了:"oh,humid" 那~还是用humid吧
- nginx socket转发设置
1.添加依赖模块,如下 --with-stream --with-stream_ssl_module 2.nginx.conf 配置,参考说明:ngx_stream_core_module user ...
- Xcode 6 下添加pch头文件
没错了,Xcode 6 有着许多坑,例如新建的工程里没有默认的pch文件,当然本质上应该是为了提高编译的速度,但却让开发略微有点不方便. 话不多说,其实新建很简单 1.先新建一个PCH文件 2.设置头 ...