LeetCode Remove Duplicated III: TreeMap Solution.
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null) {
return false;
}
TreeMap<Long, Integer> map = new TreeMap<Long, Integer>();
for(int i = 0; i < nums.length; i++) {
if(i > k) {
map.remove((long)nums[i-k-1]);
}
long val = (long)nums[i];
Long greater = map.ceilingKey(val);
if(greater != null && greater - val <= t) {
return true;
}
Long smaller = map.lowerKey(val);
if(smaller != null && val - smaller <= t) {
return true;
}
map.put(val, i);
}
return false;
}
}