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.

 

class Solution:
    # @param {integer[]} nums
    # @param {integer} k
    # @return {boolean}
    def containsNearbyDuplicate(self, nums, k):
        length = len( nums )
        if length <= 1:
            return False
        if k <= 0:
            return False
        arrMap = {}
        for i in range(0, length):
            if arrMap.has_key( nums[i] ):
                j = arrMap[nums[i]]
                if i - j <= k:
                    return True
            arrMap[nums[i]] = i
        return False

 

相关文章:

  • 2021-06-12
  • 2021-07-25
  • 2021-08-20
  • 2021-07-22
猜你喜欢
  • 2021-07-09
  • 2021-09-04
  • 2021-06-16
  • 2022-01-24
  • 2021-06-16
  • 2021-07-05
相关资源
相似解决方案