class Solution:
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
mydict={}
for i in range(len(nums)):
n=nums[i]
if n in mydict:
if i-mydict[n]<=k:
return True
else:
mydict[n]=i
else:
mydict[n]=i
return False
python leetcode 219. Contains Duplicate II
最新推荐文章于 2025-05-19 23:33:09 发布
本文介绍了一种使用字典实现的高效算法,用于检测数组中是否存在距离不超过k的重复元素。通过遍历数组并利用字典记录元素最后一次出现的位置,可以在O(n)时间内完成检测。


557

被折叠的 条评论
为什么被折叠?



