方法一:哈希表
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
my_dict = {}
for i in range(len(nums)):
if nums[i] in my_dict and i - my_dict[nums[i]] <= k:
return True
my_dict[nums[i]] = i
# for i,value in enumerate(nums):
# if value in my_dict and i - my_dict[value] <= k:
# return True
# my_dict[value] = i
return False
方法二:滑动窗口
后期补上