Python 94% hash table solution

  • 1
        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
                    if abs(i - a[nums[i]]) <= k:
                        return True
                        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.