<LeetCode><Easy> 219 ContainDuplicate 2

来源:互联网 发布:可乐冰cV淘宝店 编辑:程序博客网 时间:2024/06/06 07:09

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 difference between i and jis at most k.

#Python2 72ms

class Solution(object):    def containsNearbyDuplicate(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: bool        """        nDict={}        for e2,n in enumerate(nums):            try:                e1=nDict[n]                if e2-e1<=k:return True                else:nDict[n]=e2            except:                nDict[n]=e2        return False


1 0