Skip to content

Commit e68a3df

Browse files
committed
Modified Contains Duplicate III.java
1 parent bafda67 commit e68a3df

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

Medium/Contains Duplicate III.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
2-
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3-
TreeSet<Long> treeSet = new TreeSet<>();
4-
for (int i=0; i<nums.length; i++) {
5-
long num = nums[i];
6-
Long floor = treeSet.floor(num + t);
7-
Long ceil = treeSet.ceiling(num - t);
8-
9-
if (floor != null && floor >= nums[i] ||
10-
ceil != null && ceil <= nums[i]) {
11-
return true;
12-
}
13-
14-
treeSet.add(num);
15-
16-
if (i >= k) {
17-
treeSet.remove((long) nums[i - k]);
18-
}
19-
}
20-
21-
return false;
2+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
TreeSet<Long> set = new TreeSet<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
long num = nums[i];
6+
Long floor = set.floor(num + t);
7+
Long ceil = set.ceiling(num - t);
8+
if ((floor != null && floor >= num) || (ceil != null && ceil <= num)) {
9+
return true;
10+
}
11+
set.add(num);
12+
if (i >= k) {
13+
set.remove((long) nums[i - k]);
14+
}
2215
}
16+
return false;
17+
}
2318
}

0 commit comments

Comments
 (0)