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. ListView控件--2016年12月9日

    ListView属性 ListView   名称 说明 AccessKey 重写 WebControl.AccessKey 属性. 不支持将此属性设置 ListView 控件.(覆盖 WebContr ...

  2. ubuntu 搞坏了sudoers文件之修复方案

    pkexec visudo askubuntu原回答摘抄如下 On a modern Ubuntu system (and many other GNU/Linux distributions), f ...

  3. java项目上线过程

    关于如何将Javaweb上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. 1.在myeclipse中开发好项目,打包成war格式,不会的同学参考以下 http://z ...

  4. plist文件里边如果最外层是字典的话,读出来是无序的。

    如题. 要想使字典有序的话,可以用数组来存放字典,然后读

  5. docker--初体验

    docker这个词已经越来越热,很多docker的概念是从周围人和知乎上看的,打算从功能上先了解docker,深入的话放在以后. ps:正好手头有一台腾讯云的vps,单核,1G内存,可以拿来做实验,腾 ...

  6. 编译器--__attribute__ ((packed))

    1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...

  7. mysql循环操作

    #如果存在jyf_test存储过程则删掉 DROP PROCEDURE IF EXISTS jyf_test; DELIMITER // #创建存储过程 CREATE PROCEDURE jyf_te ...

  8. APP里如何添加本地文本

    首先考虑到用webview加载,那么久需要把文本转化成html的形势啊:如下,先把文字放到一个文本里,然后 文本转换H5: 1.在word中将文件格式布局 2.word中文本标题设为宋体标题,设字号, ...

  9. wpf的UserControl用户控件怎么添加到Window窗体中

    转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html 我们来新建一个用户控件UserControl1.xaml &l ...

  10. 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)

    前些日子工作不太忙,自己开发了一个web框架用于以后快速开发,现在分享出来. 系统没有使用各种复杂的东西,也没有太多的层次,有兴趣的可以研究一下.