Contains_Duplicate_III.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:leetcode 作者: deepdarkness 项目源码 文件源码
def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        import bisect
        l = len(nums)
        if k < 1 or t < 0 or not nums or l < 2:
            return False
        k+=1
        win=nums[:k]
        win.sort()

        for i in range(len(win)-1):
            if win[i+1]-win[i]<=t:
                return True

        for x in range(l-k):
            win.remove(nums[x])
            bisect.insort(win,nums[k+x])
            pos=bisect.bisect_left(win,nums[k+x])
            if pos>0 and win[pos]-win[pos-1]<=t:
                return True
            l=len(win)
            if pos<l-1 and win[pos+1]-win[pos]<=t:
                return True
        return False
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号