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.

这个类似前面两篇博客,只不过这里距离小于k的两个数的值小于t就可以满足要求,返回true。 这里使用multiset来实现,因为其底层使用的是红黑数,一斤排好序了,而且查找性能好,不会出现out of time的情况,主要的想法是遍历vector,当multiset的大小小于k的时候插入,判断当前值在整个set中是否有值与其相差t及以下,由于存在lower_bound,还是比较方便的。

 class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
multiset<long long> ret;
int sz = nums.size();
for (int i = ; i < sz; ++i){
if (ret.size() == k + )
ret.erase(ret.find(nums[i - k -]));
auto lb = ret.lower_bound(nums[i] - t); // 这个表达式规定了*lb - nums[i] > -t
if (lb != ret.end() && *lb - nums[i] <= t)return true; //这个规定了*lb - num[i] < t;
ret.insert(nums[i]);
}
return false;
}
};

感觉以前写的c++的版本写的比较怪,下面是java写的,其实维持一个长度为k的窗口不一定需要用multiSet直接使用java中的treeSet接可以维持一个,lower_bound以及upper_bound函数实际上也是不需要的,代码如下所示:

 public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(k < 1|| t < 0 || nums == null || nums.length < 2)
return false;
SortedSet<Long> set = new TreeSet<Long>();
for(int i = 0; i < nums.length; ++i){
SortedSet<Long> subSet = set.subSet((long)nums[i] - t, (long)nums[i] + t + 1);
if(!subSet.isEmpty())
return true;
if(i >= k){//维持一个长度为k的窗口,就是说窗口一只向前滑动
set.remove((long)nums[i-k]);
}
set.add((long)nums[i]);
}
return false;
}
}

LeetCode OJ:Contains Duplicate III(是否包含重复)的更多相关文章

  1. LeetCode 219. Contains Duplicate II (包含重复项之二)

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

  2. [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 ...

  3. LeetCode OJ:Contains Duplicate(是否包含重复)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  4. [LeetCode] 316. Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  5. 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 ...

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

  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 suc ...

  8. 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] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  10. [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 ...

随机推荐

  1. Hexo+yilia博客首页不显示全文,显示more,截断文章。

    个人主页:https://www.yuehan.online hexo new “xxx” 在md文档中 插入<!--more-->即可. 现在博客:www.wangyurui.top

  2. hexo+yilia页脚添加总访问量

    个人博客:https://www.yuehan.online 现在博客:http://www.wangyurui.top 脚本方法使用不蒜子计数 安装脚本 要使用不蒜子(官网:http://busua ...

  3. ZRGGBS00 GGB1替代问题

    ZRGGBS00ZRGGBS00ZRGGBS00 和Validation不同的是,Validation只做检测,一般不做相应数据的修改,Substitution弥补了这反面的缺陷,它和user exi ...

  4. android开发之网络棋牌类在线游戏开发心得(服务器端、Java) 好文章值得收藏

    标签: android服务器 2013-10-09 17:28 3618人阅读 评论(0) 收藏 举报 分类: android(11) 转自:http://blog.csdn.net/bromon/a ...

  5. 【Tech】POI标签分类

    寒假老板给的任务,让我重现这个实验http://www.liuhaihua.cn/archives/15565.html.自己就随便试了下,用的都是比较经典(lao)的算法和知识,记录一下. 一.从网 ...

  6. 【Tech】Mac上安装MAMP打开本地网页

    不知道为什么实验室老是用些奇葩的东西,这次是madserve,主要是用来统计移动端广告点击率的,基于PHP/MYSQL实现. 昨天很快在Windows上搭好一个xampp,并用它建立了一个virtua ...

  7. python中编写带参数decorator

    考察上一节的 @log 装饰器: def log(f): def fn(x): print 'call ' + f.__name__ + '()...' return f(x) return fn 发 ...

  8. Python编程-多进程二

    7.进程间通信(IPC)方式二:管道 (1)创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象 ...

  9. 20165101刘天野 2017-2018-2 《Java程序设计》第6周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章:常用实用类 String类:不可变类,一些看起来能够改变String的方法其 ...

  10. Logger日志级别说明及设置方法、说明

    日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...