Leetcode220 存在重复元素III

  两个关键点,一是在滑动窗口内查找元素, 二是在区间内查找元素。

  滑动窗口内查找元素可以使用哈希表维护滑动窗口,保持 O(1) 的查询效率。

  区间内查找元素可以使用有序的数据结构,查找出与元素相邻的前后两个元素,判断他们是否在区间内。

    /**
     * @Author Niuxy
     * @Date 2020/7/5 8:54 下午
     * @Description Hash 表查找,O( N * t )
     */
    public final boolean containsNearbyAlmostDuplicate0(int[] nums, int k, int t) {
        Set<Integer> window = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <= t; j++) {
                int target0 = nums[i] + j;
                if (window.contains(target0)) {
                    return true;
                }
                int target1 = nums[i] - j;
                if (window.contains(target1)) {
                    return true;
                }
            }
            window.add(nums[i]);
            if (window.size() > k) {
                window.remove(nums[i - k]);
            }
        }
        return false;
    }

  优化区间内查找元素的部分:

    /**
     * @Author Niuxy
     * @Date 2020/7/5 9:05 下午
     * @Description 二叉排序树查找最近元素,O( N * 2logN )
     */
    public final boolean containsNearbyAlmostDuplicate1(int[] nums, int k, int t) {
        TreeSet<Integer> window = new TreeSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            Integer pre = window.floor(nums[i]);
            if (pre != null && nums[i] <= t + pre) {
                return true;
            }
            Integer next = window.ceiling(nums[i]);
            if (next != null && next <= t + nums[i]) {
                return true;
            }
            window.add(nums[i]);
            if (window.size() > k) {
                window.remove(nums[i - k]);
            }
        }
        return false;
    }
原文地址:https://www.cnblogs.com/niuyourou/p/13251845.html