Python solution, easy to understand


  • 0
    Q
    class Solution(object):
        
        def containsNearbyDuplicate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: bool
            """
            d = {}
            n = 0
            for num in nums:
                if num in d:
                    if n-d[num]<=k:
                        return True
                d[num] = n
                n += 1
            return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.