Leetcode_220 Contains Duplicate III
2015-06-15 11:42
627 查看
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.
查找nums[i] -nums[j] <=t并且i-j<=k,始终保持一个长度为k的集合,每次判断该集合里是否存在与该元素相差t的元素,考虑到需要排序可直接利用有序集合SortedSet然后截取符合元素的子集合
查找nums[i] -nums[j] <=t并且i-j<=k,始终保持一个长度为k的集合,每次判断该集合里是否存在与该元素相差t的元素,考虑到需要排序可直接利用有序集合SortedSet然后截取符合元素的子集合
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(k<1 || t<0 || nums==null || nums.length<2) return false; SortedSet<Integer> set = new TreeSet<Integer>(); for(int j=0; j<nums.length; j++) { SortedSet<Integer> subSet = set.subSet(nums[j]-t, nums[j]+t+1); if(!subSet.isEmpty()) return true; if(j>=k) { set.remove(nums[j-k]); } set.add(nums[j]); } return false; }
相关文章推荐
- xhzengAIB/MessageDisplayKit
- 从浏览器中检测、安装、启动Air程序
- 硬件raid和软件raid说明、设置及维护
- Sublime Test 解决 报please wait a bit while pyV8 binary 错误
- 人工智能论坛
- 人工智能论坛
- Capistrano 3 实现Rails 自动化部署
- 再谈应用环境下的TIME_WAIT和CLOSE_WAIT
- 发现大量的TIME_WAIT解决办法
- DELET语句与REFERENCE约束“FK_news_category”冲突,该冲突发生于数据库“newssystem”,表dbo.news,column 'caId'语句终止
- LeetCode70:Climbing Stairs
- 记录一篇人工智能的内容!
- Daily mood12
- HDU Repair the Wall (贪心)
- HDOJ 1022 Train Problem I
- LeetCode ContainsDuplicate
- Uva - 442 - Matrix Chain Multiplication
- Uva - 442 - Matrix Chain Multiplication
- Leetcode 11 Container With Most Water
- uvalive 4394 string painter (序列dp)