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. Disable multi finger touch in my app

    http://stackoverflow.com/questions/12777435/disable-multi-finger-touch-in-my-app android:splitMotion ...

  2. 【转】BitKeeper与Linux,git史前琐事

    http://www.path8.net/tn/archives/6039 维持数年的BitKeeper与Linux的关系最终还是落入了好莱坞明星婚姻式的结局.他们曾经相得益彰,最后却走到这个遗憾的地 ...

  3. nodejs gearman redis

    export NODE_PATH=/root/gearman-1.1.2/node_modulesnpm install gearmanodenpm install redis w.js var re ...

  4. [GO编程] GO入门语法基础

    学习一门语言,首先肯定是要熟悉他的语法,然后才可以进行编程开发,虽然本人使用过C++,.net等语言,不过对于GO的一些新特性还是需要多多熟悉,否则即使看得懂也写不出程序来.今天我们就开始我们的GO ...

  5. 永久删除 tadb.exe

    系统安装qq手机管家等手机管理软件之后,在开机时会自动运行tadb.exe, 这个垃圾进程会让开发android程序时默认的 adb.exe启动不了,这样就没法用手机调试. 这并不是说tadb.exe ...

  6. 声色贴生成图片总结 Imagick

    2014-08-24 都是按以前的程序进行了,但去年8月都可以用Imagick正常生成CMYK的图片,但今天就是不行. 经过一切测试方法及思路,解决方法如下. 问题主要出现在: 生成的二维码是RGB格 ...

  7. java/.net-常用工具下载地址&常用学习网址&快捷键

    HTML5 HTML5:http://www.html5cn.org/ php常用网址 thinkphp框架:http://www.thinkphp.cn/ wampserver开发服务器:http: ...

  8. 寻找倒数第K个结点

    #include<stdio.h> #include<iostream> using namespace std; /** * 找到链表中的倒数第k个节点 */ //定义结构体 ...

  9. ASP.NET MVC验证标注的扩展-checkbox必选

    我们知道ASP.NET mvc提供一些表单的验证标注,比如必填属性RequiredAttribute 但是这个属性不适合选择框的必选 但是很多时候,我们却是需要一些必选的单选框 比如网站注册的时候,需 ...

  10. iOS中生成并导入基于Swift编程语言的Framework

    从iOS 8.0开始就引入了framework打包方式以及Swift编程语言.我们可以主要利用Swift编程语言将自己的代码打包成framework.不过当前Xcode 7.x在自动导入framewo ...