Leetcode学习(28)—— Contains Duplicate II

来源:互联网 发布:数据建模 pdf 编辑:程序博客网 时间:2024/06/08 18:22

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

# -*- coding:utf-8 -*-class Solution(object):    def containsNearbyDuplicate(self, nums, k):        if len(nums) <= 1:            return False        dic = {}        for key, value in enumerate(nums):            if value in dic and key - dic[value] <= k:                return True            dic[value] = key        return False

这里写图片描述

0 0