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.

解题思路:

暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型!

JAVA实现如下:

import java.util.*;
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null || nums.length<2||k<1 || t<0)
return false;
SortedSet<Long> set = new TreeSet<Long>();
for(int j=0; j<nums.length; j++) {
SortedSet<Long> subSet = set.subSet((long)nums[j]-t, (long)nums[j]+t+1);
if(!subSet.isEmpty())
return true;
if(j>=k)
set.remove((long)nums[j-k]);
set.add((long)nums[j]);
}
return false;
}
}

Java for LeetCode 220 Contains Duplicate III的更多相关文章

  1. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

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

  2. (medium)LeetCode 220.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] 220. Contains Duplicate III

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

  4. LeetCode 220. Contains Duplicate III (分桶法)

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

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java实现 LeetCode 220 存在重复元素 III(三)

    220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...

  7. 【LeetCode】220. Contains Duplicate III

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

  8. 220. Contains Duplicate III

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

  9. 220 Contains Duplicate III 存在重复 III

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...

随机推荐

  1. 性能:15个JavaScript本地存储技术的函数库和工具

    当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...

  2. 使用Minify来优化网站性能

    Minify 是用PHP5开发的应用,通过遵循一些Yahoo的优化规则来提高网站的性能.它会合并多个CSS或者JavaScript文件,移除一些不必要的空格和注释,进行gzip压缩,并且会设置浏览器的 ...

  3. KVM虚拟机内存不足,调整参数

    Dec :: vgfs001 kernel: tiotest_AMD_x86 invoked oom-killer: gfp_mask=, oom_adj=, oom_score_adj= Dec : ...

  4. 1、Java背景、标示符、运算符、转义字符

    一.Java平台: 1.Java的创建:1991年由SUN公司创建. 2.Java的特点:面向对象.可移植性.多线程.分布式.可靠.安全. 3.Java的三个架构:JavaEE.JavaSElect. ...

  5. WP8微信5.3开始内测 支持Cortana语音 两微破冰了?

    WP版微信v5.3内测版昨发布了,进行了一些小幅升级,最意外的是原生支持WP8.1版Cortana语音命令操作.要知道微软的聊天机器人“小冰”在微信上存在不到4天,就被微信全面封杀退出,现在微信又内测 ...

  6. Mac Pro 编译安装 Nginx 1.8.1

    #下载相关源码包,统一放到 /usr/local/src 目录下: http://nginx.org/download/nginx-1.8.1.tar.gz http://zlib.net/zlib- ...

  7. Android 实现简单音乐播放器(二)

    在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...

  8. Effective Java 读书笔记之十 序列化

    一.谨慎地实现Serializable接口 1.一旦一个类被发布,就大大地降低了“改变这个类的实现”的灵活性. 2.仔细设计类的序列化形式而不是接受类的默认虚拟化形式. 3.反序列化机制是一个“隐藏的 ...

  9. js循环添加事件的问题

    1.需求 给下面每个按钮增加事件 <ul id="list"> <li>按钮1</li> <li>按钮2</li> &l ...

  10. class training

    实验3-1 分别使用while循环.do while循环.for循环求 (即求1+2+3+ --+100). 参考: 源码 方法一#include<stdio.h> int main(){ ...