algorithm notes: leetcode#219 contains duplicate 2

Problem


Analysis


Solution


1
2
3
4
5
6
7
8
9
10
11
12
13
class :
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
lookup = dict()
for i, num in enumerate(nums):
if num in lookup and i - lookup[num] <= k:
return True
lookup[num] = i
return False

219. Contains Duplicate II