Very clean and neat java solution


  • 0
    F
        public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) {
            TreeSet<Integer> treeSet = new TreeSet<>();
            for (int i = 0; i < nums.length; i++) {
                Integer low = treeSet.floor(nums[i]);
                Integer high = treeSet.ceiling(nums[i]);
                if ((low != null && (long) nums[i] - low <= t) || (high != null && (long) high - nums[i] <= t)) {
                    return true;
                }
                treeSet.add(nums[i]);
                if (i >= k) {
                    treeSet.remove(nums[i - k]);
                }
            }
            return false;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.