Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
Solution
publicclassSolution {publicbooleancontainsNearbyAlmostDuplicate(int[] nums,int k,int t) {if (nums ==null||nums.length<2|| k ==0||t<0)returnfalse;int i =0;TreeSet<Long> set =newTreeSet<>();for (int j =0; j <nums.length; j++) {long cur = nums[j];long left = cur - t;long right = cur + t +1;SortedSet sortedSet =set.subSet(left, right);if (!sortedSet.isEmpty()) {returntrue; }set.add(cur);if (set.size() >= k +1) {set.remove((long)nums[i++]); } }returnfalse; }}