给定一个整数数组与一个整数k,当且仅当存在两个不同的下标i和j,满足:nums[i] = nums[j],且| i - j | <= k时返回true,反之返回false。
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
Dict = dict()
for i in range(len(nums)):
if nums[i] in Dict:
j = Dict[nums[i]]
if i - j <= k:
return True
Dict[nums[i]] = i
return False