219. 存在重复元素 II - 力扣(LeetCode)
class Solution:def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:seen = set()for i, num in enumerate(nums):if num in seen:return Trueseen.add(num)if len(seen) > k:seen.remove(nums[i - k])return False
219. 存在重复元素 II - 力扣(LeetCode)
class Solution:def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:seen = set()for i, num in enumerate(nums):if num in seen:return Trueseen.add(num)if len(seen) > k:seen.remove(nums[i - k])return False