Simple Python Solution using Set


  • 2
    Z
    class Solution(object):
        def containsNearbyDuplicate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: bool
            """
            if k>=len(nums):
                return len(set(nums)) < len(nums)
            i=0
            while i+k < len(nums):
                s = set(nums[i:i+k+1])
                if len(s)<=k:
                    return True
                    break
                i+=1
            
            return False

Log in to reply
 

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