220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 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的更多相关文章
- [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 ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [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 ...
- xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误
xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误 一:场景 xcode 同时引入了 libA.a, libB.a 两个静态类库,如果 这两个静态类库之中 ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 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 ...
- 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 ...
- (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 ...
- 【LeetCode】220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- discuz的php7版本号
php7的安装 wget http://am1.php.net/get/php-7.0.4.tar.gz/from/this/mirror tar zvxf php-7.0.4.tar.gz cd p ...
- poj1664 dp记忆化搜索
http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...
- C++对象模型——解构语意学(第五章)
5.4 对象的效率 (Object Efficiency) 在下面的效率測试中,对象构造和拷贝所须要的成本是以Point3d class声明为基准,从简单形式逐渐到复杂形式,包含Plain Ol ...
- hduoj2094产生冠军
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- mysql13---索引使用注意
.4唯一索引 ①当表的某列被指定为unique约束时,这列就是一个唯一索引 ) unique); 这时, name 列就是一个唯一索引. unique字段可以为NULL,并可以有多NULL(,null ...
- apply current folder view to all folders
https://www.tenforums.com/tutorials/35093-apply-folder-view-all-folders-same-type-windows-10-a.html ...
- windows系统下mysql5.5查看和设置数据库编码
1.显示当前编码命令: show variables like 'char%'; 2.设置编码为utf8命令:set names 'utf8';
- iOS7 push/pop转场动画
前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见 ...
- Oracle sql执行计划解析
Oracle sql执行计划解析 https://blog.csdn.net/xybelieve1990/article/details/50562963 Oracle优化器 Oracle的优化器共有 ...
- [HEOI 2012] 采花
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2743 [算法] 首先预处理nxt[]数组 , 其中 , nxt[i]表示下一个和i号 ...