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 ...
随机推荐
- 基于nodejs的wiki系统
jingo: https://github.com/claudioc/jingo nodewiki: https://github.com/nhoss2/nodewiki Tidd ...
- MC20模块教程目录
MC20模块使用教程 为了阅读和修正方便,所有教程在线观看,请在有网络的环境下观看下面教程,谢谢! MC20模块教程在线目录 第一章:基础使用,使用电脑调试MC20的各项功能 1.1 使用电脑测试MC ...
- DP专题(不定期更新)
1.UVa 11584 Partitioning by Palindromes(字符串区间dp) 题意:给出一个字符串,划分为若干字串,保证每个字串都是回文串,同时划分数目最小. 思路:dp[i]表示 ...
- SourceTree的基本使用---基本介绍/本地开发
转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ...
- 算法寒假实习面试经过之 滴滴(电话一面二面 offer)
一面:1h 介绍比赛项目. lr与xgb的区别? xgb 为什么不用归一化,onehot? xgb 与 gbdt的区别. 做这些比赛你们的优势在哪,既然全是相同的套路. RCNN的原理, CNN的原理 ...
- iOS 学习@autoreleasepool{}
" ojc-c 是通过一种"referring counting"(引用计数)的方式来管理内存的, 对象在开始分配内存(alloc)的时候引用计数为一,以后每当碰到有al ...
- 每天一个Linux命令(47)route命令
Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table). (1)用法: 用法: route ...
- I.MX6Q(TQIMX6Q/TQE9)学习笔记——U-Boot移植
其实Freescale的BSP移植文档已经将u-boot的移植步骤讲述的非常详细了,但为了以后方便查阅,还是按照自己的理解记录在这里. 获取源码 根据前一篇文章搭建好LTIB环境后就可以非常方便的导出 ...
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- 理解Android Build系统【转】
本文转载自:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/ Android Build 系统是用来编译 And ...