contains-duplicate-iii.py 文件源码

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

项目:lc-all-solutions 作者: csujedihy 项目源码 文件源码
def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        if k == 0:
            return False
        bst = []
        if k < 0 or t < 0:
            return False
        for i, num in enumerate(nums):
            idx = bisect.bisect_left(bst, num)    
            if idx < len(bst) and abs(bst[idx] - num) <= t:
                return True
            if idx > 0 and abs(bst[idx - 1] - num) <= t:
                return True
            if len(bst) >= k:
                del bst[bisect.bisect_left(bst, nums[i - k])]
            bisect.insort(bst, num)
        return False
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号