Solution URL
https://leetcode.com/submissions/detail/423687423/
代码
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
//time: O(NlogN)
//Space: O(N)
TreeSet<Long> set = new TreeSet<>();
for(int i = 0; i < nums.length; i++){
//check the largest value in the window
Long a = set.ceiling((long)nums[i]);
if(a != null && a - nums[i] <= t) return true;
//check the smallest value in the window
Long b = set.floor((long)nums[i]);
if(b != null && nums[i] - b <= t) return true;
set.add((long)nums[i]);
//keep the size of slide windows less than k
if(set.size() > k){
set.remove((long)nums[i - k]);
}
}
return false;
}
}