220. Contains Duplicate III


Given an array of integers, find out whether there are two distinct indicesiandjin the array such that the difference betweennums[i]andnums[j]is at mosttand the difference betweeniandjis at mostk.

维护一个windon

treeset:

ceiling(Ee)

Returns the least element in this set greater than or equal to the given element, ornullif there is no such element.
floor(Ee)
Returns the greatest element in this set less than or equal to the given element, ornullif there is no such element.
remove(Objecto)
Removes the specified element from this set if it is present.
public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums.length < 2 || t < 0) return false;
        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int i = 0 ; i < nums.length; i++){
            Integer floor = set.floor(nums[i] + t);
            Integer ceil = set.ceiling(nums[i] - t);
            if(floor != null && floor >= nums[i] || ceil != null && ceil <= nums[i])
                return true;
            set.add(nums[i]);
            if(i >= k) set.remove(nums[i-k]);
        }
        return false;
    }
}

优质内容筛选与推荐>>
1、網站的性能測試(一)
2、UVa 11100 The Trip, 2007
3、opencv2函数学习之erode、dilate:图像腐蚀和膨胀
4、Kafka-Docker:使用Docker运行Apache Kafka的步骤
5、关于放屁的科学道理


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn