Posted on 2022-10-22 06:13
Uriel 閱讀(55)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
游標.移動窗口
判斷一列數里面有沒有下標距離<=k的兩個相同的數,開個字典標記截至目前某個數最后出現的下標,python字典查找復雜度O(1)
1 class Solution(object):
2 def containsNearbyDuplicate(self, nums, k):
3 """
4 :type nums: List[int]
5 :type k: int
6 :rtype: bool
7 """
8 pos = {}
9 for i in range(len(nums)):
10 if nums[i] in pos and i - pos[nums[i]] <= k:
11 return True
12 pos[nums[i]] = i
13 return False