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 difference between i and j is at most k.
classSolution(object): defcontainsNearbyDuplicate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: bool """ d = {} # 用来存放hash表 for i in range(len(nums)): if nums[i] in d: # 如果hash表中已经存在该值 if i - d[nums[i]] <= k: # 和C++,Java的map不同,这里的字典可以覆盖,不必删除 returnTrue d[nums[i]] = i returnFalse