[Leetcode] Contains Duplicate III
2024-09-13 14:27:53
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.
SOL1:
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums == null || nums.length == 0 || t < 0)
return false;
TreeSet<Long> ts = new TreeSet<>();
for(int i = 0; i < nums.length; ++i) {
if(i > k)
ts.remove((long)nums[i - k - 1]);
TreeSet<Long> temp = (TreeSet<Long>) ts.subSet((long)nums[i] - t, true,(long)nums[i] + t, true);
if(!temp.isEmpty())
return true;
ts.add((long)nums[i]);
}
return false;
}
}
SOL2:
利用bucket来做:
https://leetcode.com/discuss/38206/ac-o-n-solution-in-java-using-buckets-with-explanation
最新文章
- AndRodi Strudio中的按钮时件
- java 给指定时间加上天数or给当前日期加天数
- centos6.5 iptables结合ipset批量屏蔽ip
- 程序猿的编程神器 - vim
- 利用switch语句进行多选一判断。
- java7 - JDK
- python 中的csv读写
- 分部视图(Partial View)及Html.Partial和Html.Action差异
- UOJ#422 小Z的礼物
- (转)Golang--使用iota(常量计数器)
- WebLogic登录管理控制台、以及相关问题解决
- CF939D Love Rescue 并查集
- 本地用maven搭建SpringMvc+redis集成
- PL/SQL报错:Initialization error Oracle client not properly installed
- 51nod1126 求递推序列的第N项
- Cocos2d-x教程(31)-TableView的滚动栏
- 关于MySQL的几个命令之load
- CB XE7 C11 64位编译器 成员变量初始化
- 线段树---poj2528 Mayor’s posters【成段替换|离散化】
- bzoj2938 病毒