class Solution:
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
a_dict={}
for i in range(len(nums)):
if nums[i] not in a_dict:
a_dict[nums[i]] = i
else:
if i - a_dict[nums[i]] <= k:
return True
else:
a_dict[nums[i]] = i
return False
No comments:
Post a Comment