Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
给一个数组,求两个不相等的i和j使得nums[i] == nums[j] 并且 j - i <= k
class Solution(object): def containsNearbyDuplicate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: bool """ d = {} for i in range(len(nums)): value = nums[i] if value in d and i - d[value] <= k: return True d[value] = i return False