给定一个整数数组,判断数组中是否有两个不同的索引 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. Servlet的引入

    一.分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuService = new StudentServiceImpl(); List<Student ...

  2. Part1-Redefining your data-access strategy 重新定义你的数据访问策略

    欢迎来到Entity Framework 4 In Action,EF是微软3.5 SP1推出的ORM工具,现在已经更新到4.0版本(...)本书能确保你in a  robust and model- ...

  3. 将项目上传到GitHub

    第一步: 1.进入Github首页,点击New repository新建一个项目 2.填写相应信息后点击create即可 Repository name: 仓库名称 Description(可选): ...

  4. HBase在大搜车金融业务中的应用实践

    摘要: 2017云栖大会HBase专场,大搜车高级数据架构师申玉宝带来HBase在大搜车金融业务中的应用实践.本文主要从数据大屏开始谈起,进而分享了GPS风控实践,包括架构.聚集分析等,最后还分享了流 ...

  5. make运行阶段划分

    1 make执行分为两个阶段 第一个阶段:读makefile并且建树阶段 第二个阶段:构建目标阶段 2 扩展的立即和推迟 在第一个阶段的扩展是立即,在第二个阶段或者在需要的时候再扩展是推迟,这里的需要 ...

  6. eclipse 显示行数

    在左侧添加断点的 地方右击 选择 Show Line Numbers

  7. java的Date日期使用

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  8. Lightoj 1020 - A Childhood Game

    Allice先拿,最后拿球的输. Bob先拿,最后拿球的赢. 考虑Alice先拿球,当n=1时 Alice输  记dp[1]=0; n=2,  dp[2]=1 n=3,  dp[3]=1 因为n=1, ...

  9. 并不对劲的uoj276. [清华集训2016]汽水

    想要很对劲的讲解,请点击这里 题目大意 有一棵\(n\)(\(n\leq 50000\))个节点的树,有边权 求一条路径使该路径的边权平均值最接近给出的一个数\(k\) 输出边权平均值下取整的整数部分 ...

  10. 就是要第一个出场的albus 【BZOJ】 线性基

    就是我代码里读入之后的那一部分. 1.(一下a[]为原数组 a'[]为线性基) 线性基 中的a'[i]其实 是 原来的a[]中的某个子集(2^n个子集中的某个) 异或出来的  可能会有其他的子集与它异 ...