Python solution using sets, O(k) space


  • 0
    V
        def containsNearbyDuplicate(self, nums, k):
            h = set([])
            j = -(k+1)
            for i in xrange(len(nums)):
                if j>=0:
                    h.discard(nums[j])
                if nums[i] in h:
                    return True
                h.add(nums[i])
                j+=1
                
            return False
    

Log in to reply
 

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