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. scrapy使用笔记

    新建项目 在需要新建项目的目录下发指令 scrapy startproject MySpider 其中MySpider为工程的名字,会新建一个文件夹 进入工程目录 新建一个爬虫 scrapy gens ...

  2. (from) Javascript 生成指定范围数值随机数

    from:http://blog.csdn.net/ilibaba/article/details/3741786 查手册后才知道, 介绍的信息少得可怜呐, 没有介绍生成 m-n 范围的随机数..., ...

  3. XSS - 禁止浏览器读取Cookie - HttpOnly

    1.什么是HttpOnly? 如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索.   C ...

  4. OC知识点(类方法,构造方法,组合模式,get,set方法,自动生成属性)

    1.类方法的优势 不用创建对象,节省了空间,直接用类名调用类方法,类方法为外界提供一个方便的调用接口.(特点:类方法以加号开头,不能使用自身的成员变量,它的调用不依赖成员变量) 2.构造方法(初始化成 ...

  5. 树莓派打造对话机器人 Python(转)

    工具列表 1. **树莓派**(型号不要求,本人使用的是3B) 2. **usb麦克风**(某宝有卖,我就不打广告了) 用来录音 3. **音响或者喇叭**(某宝也有卖) 用来播放 以上就是需要的工具 ...

  6. Python自然语言处理系列之模拟退火算法

    1.基本概念 模拟退火算法(Simulated Annealing,SA)是一种模拟固体降温过程的最优化算法.其模拟的过程是首先将固体加温至某一温度,固体内部的粒子随温度上升慢慢变为无序的状态,内能增 ...

  7. Linux的Cache Memory(缓存内存)机制

    转:https://blog.csdn.net/kaikai_sk/article/details/79177036 PS:为什么Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与W ...

  8. R的基础学习之数据结构

    来源:http://blog.qiubio.com:8080/archives/3753/4 1.atomic vector :一维的,放置同一类型数据的数据类型 1.1创建:由c()函数 ,seq( ...

  9. tornado解析 第一篇

    一.tornado介绍 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 we ...

  10. iOS_KVC与KVO

    目 录: 一.KVC   二.KVO                                                                                   ...