Contains Duplicate III
2015-08-06 15:42
453 查看
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.
思路:给定一个整数数组,判断其中是否存在两个不同的下标i和j满足:|
nums[i] - nums[j] | <= t 并且 | i - j | <= k。利用TreeSet中的ceiling()方法和floor()方法,来确定滑动窗口内的是否差小于等于t
代码如下:
http://www.tuicool.com/articles/iE3imiQ
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.
思路:给定一个整数数组,判断其中是否存在两个不同的下标i和j满足:|
nums[i] - nums[j] | <= t 并且 | i - j | <= k。利用TreeSet中的ceiling()方法和floor()方法,来确定滑动窗口内的是否差小于等于t
代码如下:
public class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(nums==null || k<1 || t<0) return false; TreeSet<Integer> set = new TreeSet<>(); for(int i=0;i<nums.length;i++){ int n = nums[i]; if(set.ceiling(n)!=null&&set.ceiling(n)<=t+n || set.floor(n)!=null&& n<=t+set.floor(n)){ return true; } set.add(nums[i]); //保证窗口长度为k if(i>=k){ set.remove(nums[i-k]); } } return false; } }
http://www.tuicool.com/articles/iE3imiQ
相关文章推荐
- 杭电1702ACboy needs your help again!
- RAID 5
- Dell-硬件Raid详细图解步骤
- NSSearchPathForDirectoriesInDomains
- constraint的用法
- Xcode6 svn check out项目出现“The operation couldn’t be completed. (NSURLErrorDomain error -1012.)”的解决方案
- 2015 Multi-University Training Contest 5(HDOJ5351)
- no project are available for deployment to this server&n
- svn cleanup failed–previous operation has not finished 解决方法
- (暴力枚举) UVa 10976 Fractions Again?!
- UVa 10986 Sending email (最短路+Dijkstra队列优化)
- poj3216Repairing Company 二分匹配之最小路径覆盖+floyd
- opencontrail本地编译定义标签
- Codeforces Round #Pi (Div. 2)567A Lineland Mail(模拟)
- tair分布式缓存
- 2015 Multi-University Training Contest 5 hdu 5348 MZL's endless loop
- http://blog.csdn.net/xiaanming/article/details/17483273
- Code Forces 567 A. Lineland Mail(水~)
- URAL 1031 Railway Tickets
- CodeForces 567A Lineland Mail 贪心