Leetcode Contains Duplicate III
2015-09-22 02:19
495 查看
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.
另外,要考虑溢出情况,所以必须使用long !!!!
方法一: TreeSet 用的是Binary Search Tree, 有两个有用的method: ceiling() and floor(), the time complexity is O(nlog(k)).
ceiling() : Returns the least element in this set greater than or equal to the given element, or
floor() : Returns the greatest element in this set less than or equal to the given element, or
方法二: SortedSet 可以界定左边界和右边界。
两个方法的共同点是维护好k的窗口。
Java code:
方法一:
方法二:
Reference:
1. http://www.programcreek.com/2014/06/leetcode-contains-duplicate-iii-java/
解题思路:
关键是要想到用TreeSet或者SortedSet, 而且要维护好k 的窗口!!!另外,要考虑溢出情况,所以必须使用long !!!!
方法一: TreeSet 用的是Binary Search Tree, 有两个有用的method: ceiling() and floor(), the time complexity is O(nlog(k)).
ceiling() : Returns the least element in this set greater than or equal to the given element, or
nullif there is no such element.
floor() : Returns the greatest element in this set less than or equal to the given element, or
nullif there is no such element.
方法二: SortedSet 可以界定左边界和右边界。
两个方法的共同点是维护好k的窗口。
Java code:
方法一:
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(k < 1 || t < 0) { return false; } TreeSet<Long> set = new TreeSet<Long>(); for(int i= 0; i< nums.length; i++) { long x = (long)nums[i]; if((set.floor(x) != null && (x-(long)(set.floor(x)) <=(long)t)) || (set.ceiling(x)!=null && ((long)(set.ceiling(x))-x <=(long)t))) { return true; } set.add(x); if(i>=k) { set.remove((long)nums[i-k]); } } return false; }
方法二:
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(k < 1 || t < 0) { return false; } SortedSet<Long> set = new TreeSet<Long>(); for(int i =0; i< nums.length; i++) { long leftBound = (long)nums[i]-t; long rightBound = (long)nums[i]+t+1; SortedSet<Long> subSet = set.subSet(leftBound, rightBound); if(!subSet.isEmpty()){ return true; } set.add((long)nums[i]); if(i>=k) { set.remove((long)nums[i-k]); } } return false; }
Reference:
1. http://www.programcreek.com/2014/06/leetcode-contains-duplicate-iii-java/
相关文章推荐
- Leetcode Contains Duplicate II
- LightOJ 1236 Pairs Forming LCM(算术基本定理)
- Leetcode Contains Duplicate
- main()参数int argc,int *argv[]的学习
- Light oj 1086 - Jogging Trails(状压dp 将图变成欧拉图)
- Sublime Text 2 - There are no packages available for installation 解决方法(亲测可行)
- E: 无法获取锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)解决方法
- exit()和_exit()函数 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html
- Codeforces__Raising Bacteria
- Codeforces__Raising Bacteria
- xCode 7 编译出现错误:does not contain bitcode 解决办法
- 【转】友好的 ContainerView & AutoLayout 资料推荐
- [置顶] Linux DM9000网卡驱动程序完全分析 http://blog.csdn.net/ypoflyer/article/details/6209922
- AI-Tank
- 查询表锁争用情况 table_locks_waited & table_locks_immedaite
- setContentView(R.Layout.main)的意思
- raid5实现原理
- RAID5服务器磁盘阵列,两块盘坏了数据恢复
- http://blog.csdn.net/abcjennifer/article/details/7716281 过拟合问题
- 为什么使用HttpUrlConnection时候,用available方法不准确