219. Contains Duplicate II

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

 

posted @ 2020-07-14 22:21  whatyouthink  阅读(72)  评论(0编辑  收藏  举报