-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc220.java
More file actions
30 lines (28 loc) · 777 Bytes
/
Lc220.java
File metadata and controls
30 lines (28 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package leetcode;
import java.util.TreeSet;
/**
* @author Kuma
* @date 2021年4月17日
* 220.存在重复元素 III
*/
public class Lc220 {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
int left = 0;
int right = 0;
TreeSet<Long> set = new TreeSet<>();
int n = nums.length;
while (right < n){
if (right - left > k){
set.remove((long)nums[left]);
left++;
}
Long ceiling = set.ceiling((long)nums[right] - (long)t);
if (ceiling != null && Math.abs(ceiling - nums[right]) <= t) {
return true;
}
set.add((long)nums[right]);
right++;
}
return false;
}
}