Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 795 Bytes

0219._contains_duplicate_ii.md

File metadata and controls

34 lines (28 loc) · 795 Bytes

Navigation

Links:

  1. https://leetcode.com/problems/contains-duplicate-ii/
  2. https://leetcode-cn.com/problems/contains-duplicate-ii/

Solution 1 空间换时间, hash table

    时间复杂度:O(n)
    空间复杂度:O(min(n, k))

class Solution:
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        memo = {}

        for index, value in enumerate(nums):
            if value in memo and index - memo[value] <= k:
                return True

            memo[value] = index

        return False