LeetCode 220 Contains Duplicate III
2016-05-17 11:47
363 查看
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.
注意sortedset的应用。参考:点击打开链接
at most t and the difference between i and j is at most k.
注意sortedset的应用。参考:点击打开链接
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++) { if (!set.subSet((long) nums[i] - t, (long) nums[i] + t + 1).isEmpty()) return true; if (i >= k) set.remove((long) nums[i - k]); set.add((long) nums[i]); } return false; }
相关文章推荐
- Codeforces Round #353 (Div. 2) B. Restoring Painting 水题
- From Opentsdb To Kairosdb
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- async and await 简单的入门
- Tomcat启动报错 Failed to initialize connector [Connector[HTTP/1.1-8080]]
- Codeforces Round #353 (Div. 2)-B. Restoring Painting(数学)
- 详细解析Raid0、Raid0+1、Raid1、Raid5四者的区别
- Grails在服务类中获取web根目录
- Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
- AEAI WM V1.5.0 升级说明,开源工作管理系统
- AEAI WM V1.5.0 升级说明,开源工作管理系统
- LeetCode 336. Palindrome Pairs
- 03-树3 Tree Traversals Again
- Failed to deploy virtual device -- Genymotion安装虚拟器失败
- sleep、yield、wait、join的区别
- Spirng+In+Action(Craig Walls Ryan Breidenbach)
- CodeForces 608A Saitama Destroys Hotel(水题)
- 服务器主板s2600cp2配置Raid5全过程
- No TypeTag available for person