当前位置:网站首页>Leetcode topic analysis contains duplicate III
Leetcode topic analysis contains duplicate III
2022-06-23 09:08:00 【ruochen】
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between numsi and numsj is at most t and the difference between i and j is at most k.
For added elements , To be able to O(1) Find... In time , At the same time, it also has to sort automatically , Maintain one k An element of TreeSet.
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (k < 1 || t < 0 || nums == null || nums.length < 2) {
return false;
}
SortedSet<Long> set = new TreeSet<Long>();
for (int j = 0; j < nums.length; j++) {
SortedSet<Long> subSet = set.subSet((long) nums[j] - t,
(long) nums[j] + t + 1);
// Set is not empty , Then the solution is found
if (!subSet.isEmpty()) {
return true;
}
if (j >= k) {
set.remove((long) nums[j - k]);
}
set.add((long) nums[j]);
}
return false;
}边栏推荐
猜你喜欢
随机推荐
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
16.系统启动流程
H-index of leetcode topic analysis
社区文章|MOSN 构建 Subset 优化思路分享
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
Quartz Crystal Drive Level Calculation
【云原生 | Kubernetes篇】Kubernetes原理与安装(二)
node request模塊cookie使用
C#之Lambda不得不说的用法
MySQL fault case | mysqldump: couldn't execute 'select column_ NAME
[learning resources] understand and love mathematics
位绑定
670. Maximum Swap
Leetcode topic analysis group anagrams
125. Valid Palindrome
Combination sum of leetcode topic analysis
“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
4、 Database design








