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. 几种常用的JS类定义方法(转)

    // 方法1 对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : funct ...

  2. Linux关于添加硬盘的那些事儿:笔记

    添加新硬盘:http://note.youdao.com/share/?id=8cf27602cdce36e1d4160f00e9004b00&type=note 关于添加硬盘的那些事儿:   ...

  3. CF453C Little Pony and Summer Sun Celebration (DFS)

    http://codeforces.com/contest/456  CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...

  4. php简单实用的操作文件工具类(创建、移动、复制、删除)

    php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) {  // 原目录,复制到的目录 $dir = opend ...

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

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

  6. springmvc之定时器

    一.通过注解方式实现定时器 1.工程结构 2.所需jar包 3.spring-config.xml,springmvc配置文件 <?xml version="1.0" enc ...

  7. RFM

    前面博客中讲到的聚类,聚类是对客户的一些特征进行分群,属于描述,不涉及客户价值的判断,然而在营销中,其实第一步应该是搞清楚谁才是你的关键客户,哪些用户的价值较高,这就需要用到RFM模型.RFM模型是众 ...

  8. php正则

    PHP代码 $str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="li ...

  9. Ubuntu编译PHP7问题

    安装编译依赖 sudo apt-get -y install build-essential git autoconf sudo apt-get build-dep php5 sudo apt-get ...

  10. word文档的生成、修改、渲染、打印,使用Aspose.Words

    无需MS Word也可执行各种文档处理任务,包括文档的生成.修改.渲染.打印,文档格式转换和邮件合并等文档处理.