AC Java 36ms with TreeSet


  • 0
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            TreeSet<Integer> tree = new TreeSet<Integer>();
            for (int i = 0; i < nums.length; i++) {
                Integer h = tree.higher(nums[i]-t-1);
                if (h != null && Math.abs(h-nums[i]) <= t) return true;
                tree.add(nums[i]);
                if (i-k >= 0) tree.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.