Description:

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.

题目大意:给定一个数组,和两个数t和k。判断是否满足下列条件的数。存在两个不同的下标i,j满足: nums[i] - nums[j] | <= t 且 | i - j | <= k。

思路:使用Java中的TreeSet,TreeSet是有序的内部是红黑树实现的。并且内部带有操作方法很方便,会降低问题的复杂度。其实就是一个滑动窗口,把可能满足条件的范围从左向右滑动,直到找出满足条件的数或者窗口滑动完毕。

实现代码:

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(k < 1 || t < 0) return false; TreeSet<Integer> set = new TreeSet<Integer>(); for(int i=0; i<nums.length; i++) {
int cur = nums[i];
Integer floor = set.floor(cur);
Integer ceil = set.ceiling(cur); if(floor != null && cur <= t + floor
|| ceil != null && ceil <= t + cur) {
return true;
}
set.add(cur);
if(i >= k) {
set.remove(nums[i - k]);
}
}
return false;
}
}

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

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

  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. Ubuntu “无法获得锁”解决方案(E: 无法获得锁 /var/cache/apt/archive

    Ubuntu “无法获得锁”解决方案(E: 无法获得锁 /var/cache/apt/archive   现象一: E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源 ...

  2. [mongodb-10gen]ubuntu下安装方法

    由于自己老是不死心,所以还是继续在UBUNTU下开始我的GOLANG,今天发现服务源很快一下子就把GO的源代码从GoogleProject上给hg回来了,所以今天才查了一下在UBUNTU下安装Mong ...

  3. java微信开发API第一步 服务器接入

    I如何接入服务器,下面就为大家进行介绍 一.说明 * 本示例根据微信开发文档:http://mp.weixin.qq.com/wiki/home/index.html最新版(4/3/2016 5:34 ...

  4. GTD时间管理(3)---时间特区图

    最近在网上看到一副时间特区图,想象非常深,特点想分享给大家. 从上图可以看出 说明一个人全天的状况 说明: 全天时段 状态 7:30-8:00 是处于起床,穿衣,刷牙,吃早餐 8:00-9:00 是处 ...

  5. Hadoop学习篇1 快速入门

    Hadoop是Apache Lucene创始人Doug Cutting创建的,Hadoop起源于Apache Nutch,一个开源的网络搜索引擎.最先引起注意是2003年google的一篇论文,该论文 ...

  6. jquery scrollLeft居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Scala 深入浅出实战经典 第43讲:主要介绍类型变量bound

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  8. 在线制作h5

    在线制作h5 官网:http://www.godgiftgame.com 在线制作h5首页预览效果图如下: 一.主要功能区域主要功能区域分布在上中左右三个地方,1.上面区域是功能选择区,包括图片素材. ...

  9. android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色

    android自定义view,实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色. 由于时间比较仓促,因此没有对代码进行过多的优化,功能远远不如androi ...

  10. 深入解析DC/OS 1.8 – 高可靠的微服务及大数据管理平台

    深入解析DC/OS 1.8 – 高可靠的微服务及大数据管理平台 大家好,欢迎大家参加这次DC/OS的技术分享. 先做个自我介绍,刘超,Linker Networks首席架构师,Open DC/OS社区 ...