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

[Leetcode] Contains Duplicate III的更多相关文章

  1. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  2. LeetCode——Contains Duplicate III

    Description: Given an array of integers, find out whether there are two distinct indices i and j in ...

  3. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  4. Contains Duplicate III -leetcode

    Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...

  5. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  6. [LeetCode] Contains Duplicate 包含重复值

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  7. Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  8. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. check用户协议

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. kernel 4.4.12 EETI eGTouch 电容屏驱动移植

    kernel 4.4.12 EETI eGTouch 电容屏驱动移植: 在make menuconfig 里面添加如下选项: 添加通过事件上报接口节点: Device Drivers ---> ...

  3. 函数find(exp)

    Matlab函数find(exp): 查找符合exp的值并储存 示例: b=find(a>20&a<40) 仅此

  4. ember.js路由无效的解决思路

    进入今天的问题,就是route ember中就一个html,单页面程序(spa),所以页面的跳转,也可以叫做页面的路由,其实就是在这一个html中,不断的进行html的插入和删除了(个人理解) emb ...

  5. 【python】nuitka封装python

    官网:http://nuitka.net/doc/user-manual.html python打包工具对比:http://blog.csdn.net/qwemicheal/article/detai ...

  6. SQL添加维护 计划失败

    在sql要求数据库每天自动备份这个是大家都会遇到的问题,我遇到了这个问题如图: 是因为这个服务组件没有安装

  7. _NSInlineData objectForKeyedSubscript:

    最近总是遇到这个错误:reason: '-[_NSInlineData objectForKeyedSubscript:]: unrecognized selector sent to instanc ...

  8. springMVC中 request请求数据绑定到Controller入参 过程剖析

    前言:Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型.若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数.众 ...

  9. js 字符串操作函数

    concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定 ...

  10. Swift - UIView,UItableView,Cell设置边框方法

    // 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...