给定一个整数数组,判断数组中是否有两个不同的索引 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. MVC在View中页面跳转

    在做人事系统的时候须要用到页面跳转,那么页面跳转究竟用什么方法好呢?依照曾经的思路,我就会这么写. <span style="font-size:18px;">wind ...

  2. phpqrcode生成带logo的二维码图片及带文字的二维码图片

    <?php require_once "./phpqrcode/phpqrcode.php"; /** * 这样就可以生成二维码了,实际上在png这个方法里还有几个参数需要使 ...

  3. XMU 1607 nc与点对距离 【线段树】

    1607: nc与点对距离 Time Limit: 5000 MS  Memory Limit: 512 MBSubmit: 60  Solved: 8[Submit][Status][Web Boa ...

  4. [10.27_P3] 简单题 (脑洞)

    Description dzy 手上有一张n 个点m 条边的联通无向图,仙人掌是一张每条边最多在一个简单环内的联通无向图.他想求这个无向图的生成仙人掌中最多有多少条边. 但是dzy 觉得这个问题太简单 ...

  5. HTML5、javascript写的craps游戏

    1. [代码][HTML]代码   <!DOCTYPE HTML><html><head><meta charset="utf-8"> ...

  6. linux系统无法上外网,路由器可以上网,可以ping通路由器,ping不通外网IP

    临时生效方法(添加路由网关),执行: #route add default gw 192.168.92.1   #根据实际网关IP填写 如果不行,使用下面方法: 一:使用 route 命令添加使用ro ...

  7. bzoj 2962 序列操作 —— 线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2962 维护 sum[i] 表示选 i 个的乘积和,合并两个子树就枚举两边选多少,乘起来即可: ...

  8. Outlook 2007 发送邮件

    4 登入以投票 Hi, http://social.msdn.microsoft.com/Forums/zh-TW/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-t ...

  9. Oracle11g for CentOS6-*

    lsnrctl startsqlplus /nologstartup

  10. UI:使用 pod 引入 AFNetworking

    cocdpods的安装  参考1  参考2 参考3 注意:MVC是一种搭建项目的思想,不是设计模式. 使用第三方管理控件: 引入CocoaPods的详细步骤:(1)检测有没有引入淘宝镜像gem sou ...