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

详见:https://leetcode.com/problems/contains-duplicate-iii/description/

Java实现:

TreeSet数据结构(Java)使用红黑树实现,是平衡二叉树的一种。
该数据结构支持如下操作:
floor()方法返set中≤给定元素的最大元素;如果不存在这样的元素,则返回 null。
ceiling()方法返回set中≥给定元素的最小元素;如果不存在这样的元素,则返回 null。

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

参考:https://www.cnblogs.com/jiajiaxingxing/p/4572871.html

https://www.jianshu.com/p/3e27c6fe284e

C++实现:

class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
int size=nums.size();
if(size==0||nums.empty())
{
return false;
}
map<long long,int> m;
int j=0;
for(int i=0;i<size;++i)
{
if(i-j>k)
{
m.erase(nums[j++]);
}
auto idx=m.lower_bound((long long)nums[i]-t);
if(idx!=m.end()&&abs(idx->first-nums[i])<=t)
{
return true;
}
m[nums[i]]=i;
}
return false;
}
};

220 Contains Duplicate III 存在重复 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. [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] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  4. xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误

    xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误 一:场景 xcode 同时引入了 libA.a, libB.a 两个静态类库,如果 这两个静态类库之中 ...

  5. 220. Contains Duplicate III

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

  6. 220. Contains Duplicate III 数组指针差k数值差t

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

  7. Java for 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 ...

  8. (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 ...

  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. 【git体验】git原理及基础

    原理:分布式版本号控制系统像 Git,Mercurial,Bazaar 以及 Darcs 等,client并不仅仅提取最新版本号 的文件快照,而是把原始的代码仓库完整地镜像下来. 这么一来.不论什么一 ...

  2. 深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

    不多说,直接上干货! 深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

  3. hadoop打jar包

    编译:   javac  -classpath     hadoop的路径下面/hadoop-0.20.0-core.jar       -d     .class文件存放的路径     XXXX.j ...

  4. Deep Learning 27:Batch normalization理解——读论文“Batch normalization: Accelerating deep network training by reducing internal covariate shift ”——ICML 2015

    这篇经典论文,甚至可以说是2015年最牛的一篇论文,早就有很多人解读,不需要自己着摸,但是看了论文原文Batch normalization: Accelerating deep network tr ...

  5. vim记住上次编辑和浏览位置

    在用户自己的目录下的.vimrc中添加, "remember last update or view postion" " Only do this part when ...

  6. ACTION中获得数据的几种方式

    1.第一种是通过公司封装的方法. 2.第二种:是通过IF方法判断 3.第三种是通过:set/get获得

  7. [Django基础] django解决静态文件依赖问题以及前端引入方式

    一.静态文件依赖 学习django的时候发现静态文件(css,js等)不能只在html中引入,还要在项目的settings中设置,否则会报以下错误 [11/Sep/2018 03:18:15] &qu ...

  8. Gitlab+Gerrit+Jenkins完整对接

    近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...

  9. 【194】Windows 上使用 wget

    本文包括两部分,首先就是在 Windows 使用 wget 来下载文件,这样固然很好,然而问题并非这么简单,在 PowerShell 4.0 版本中增加了 Invoke-WebRequest 的别名 ...

  10. Pascal学生管理

    program Project2; {$APPTYPE CONSOLE} uses SysUtils; ;M=; type date=record day:..; month:..; year:..; ...