contains_duplicate_ii

  • 2022-12-14
  • 浏览 (547)

contains_duplicate_ii.py 源码

# 是否存在重复元素II
from typing import List


class Solution:

    # 暴力法,依次遍历
    def containsNearbyDuplicate_1(self, nums: List[int], k: int) -> bool:
        for i in range(len(nums) - 1):
            for j in range(i + 1, len(nums)):
                if j - i > k:
                    break
                if nums[j] == nums[i]:
                    return True
        return False

    # 使用一个哈希表
    def containsNearbyDuplicate_2(self, nums: List[int], k: int) -> bool:
        m = {}
        for i, v in enumerate(nums):
            if v in m and i - m[v] <= k:
                return True
            m[v] = i
        return False

你可能感兴趣的文章

best_time_buy_sell_stock

best_time_buy_sell_stock_ii

climb_statirs

0  赞