Python 94% hash table solution


  • 1
    I
        def containsNearbyDuplicate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: bool
            """
            a = {}
            for i in range(len(nums)):
                if nums[i] not in a:
                    a[nums[i]] = i
                else:
                    if abs(i - a[nums[i]]) <= k:
                        return True
                    else:
                        a[nums[i]] = i
            return False

Log in to reply
 

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