algorithm notes: leetcode#532 k

Problem


Analysis


Solution


1
2
3
4
5
6
7
8
9
10
11
class :
def findPairs(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
cnt = dict()
for num in nums:
cnt[num] = cnt.get(num, 0) + 1
return sum([(k>0 and num+k in cnt) or (k==0 and cnt[num]>1) for num in cnt])

532. K-diff Pairs in an Array