首页 > 其他 > 详细

219. Contains Duplicate II

时间:2020-07-15 00:54:02      阅读:41      评论:0      收藏:0      [点我收藏+]

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

 

219. Contains Duplicate II

原文:https://www.cnblogs.com/whatyouthink/p/13302244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!