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

最新文章

  1. AndRodi Strudio中的按钮时件
  2. java 给指定时间加上天数or给当前日期加天数
  3. centos6.5 iptables结合ipset批量屏蔽ip
  4. 程序猿的编程神器 - vim
  5. 利用switch语句进行多选一判断。
  6. java7 - JDK
  7. python 中的csv读写
  8. 分部视图(Partial View)及Html.Partial和Html.Action差异
  9. UOJ#422 小Z的礼物
  10. (转)Golang--使用iota(常量计数器)
  11. WebLogic登录管理控制台、以及相关问题解决
  12. CF939D Love Rescue 并查集
  13. 本地用maven搭建SpringMvc+redis集成
  14. PL/SQL报错:Initialization error Oracle client not properly installed
  15. 51nod1126 求递推序列的第N项
  16. Cocos2d-x教程(31)-TableView的滚动栏
  17. 关于MySQL的几个命令之load
  18. CB XE7 C11 64位编译器 成员变量初始化
  19. 线段树---poj2528 Mayor’s posters【成段替换|离散化】
  20. bzoj2938 病毒

热门文章

  1. bzoj4025 二分图
  2. U3D学习笔记1: HelloWorld
  3. tornado中将cookie值设置为json字符串
  4. Code::Blocks如何支持C++11特性
  5. 使用httpclient 调用selenium webdriver
  6. 第三天--html表格
  7. WinForm BaseClass类常用通用方法
  8. vim 命令
  9. css经典布局——头尾固定高度中间高度自适应布局
  10. (转)Redis使用场景及使用经验