LeetCode OJ:Contains Duplicate III(是否包含重复)
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(是否包含重复)的更多相关文章
- 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 ...
- [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 OJ:Contains Duplicate(是否包含重复)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 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 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 suc ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [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 ...
随机推荐
- [转载]MySQL concat函数的使用
MySQL concat函数是MySQL数据库中众多的函数之一,下文将对MySQL concat函数的语法和使用进行说明,供您参考和学习. MySQL concat函数使用方法:CONCAT(str1 ...
- Python学习笔记1_初识Python
一.Python的发展 1.CNRI时期:CNRI是自助Python发展初期的重要单位,Python1.5版之前的成果大部分都在此时期内完成 2.BeOpen时期:Guido van Rossum与B ...
- 使用 C#的 is 和 as 操作符来转型
在 C#语言中进行类型转换的另一种方式是使用 is 操作符. is 检查一个对象是否兼容于指定的类型,并返回一个 Boolean 值: true 或 false.注意 is 操作符永远不会抛出异常,以 ...
- mysql数据库中表记录的玩法
一.增加表记录(相当于插入表记录) 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); 语法二: INS ...
- C#托管代码 CLR
托管代码 是直接编译成机器码,而是编译成中间语言 IL,由 CLR 托管运行. 托管代码就是把底层的一些操作(如内存的读取,释放)全都封装起来了,把有关内存管理的操作全都由CLR来管理, C#使用垃圾 ...
- Android电容屏(一)【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7820492 关键词:Android 电容屏 tp ITO 平台信息:内核:linu ...
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- MAC 系列 之XCode7.1 + HBuilder MUI 离线打包 ipa 上次application leader 问题:ERROR ITMS - 90534
解决方法:这个原因 网上说法是 beta 测试版本:不过的确是beta版本(7.3 beta)打包的,所以我有下载了一个正式版本 7.1版本. 再次进行测试打包!
- Qt QSortFilterProxyModel示例代码, 使用方法
1. QSortFilterProxyModel不能单独使用,它只是一个"代理",真正的数据需要另外的一个model提供,而且它是用来排序和过滤的. 2. 实现代码 #ifndef ...
- Spring初学之通过工厂方法配置Bean
工厂方法配置bean分为两种,一种是实例工厂方法,另一种是静态工厂方法. 先来看看实体bean: Car.java: package spring.beans.factory; public clas ...