220. Contains Duplicate III
2016-12-05 07:18
274 查看
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.
维护一个windon
treeset:
Returns the least element in this set greater than or equal to the given element, or
Returns the greatest element in this set less than or equal to the given element, or
Removes the specified element from this set if it is present.
维护一个windon
treeset:
ceiling(E e)
Returns the least element in this set greater than or equal to the given element, or
nullif there is no such element.
floor(E e)
Returns the greatest element in this set less than or equal to the given element, or
nullif there is no such element.
remove(Object o)
Removes the specified element from this set if it is present.
public class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(nums.length < 2 || t < 0) return false; TreeSet<Integer> set = new TreeSet<Integer>(); for(int i = 0 ; i < nums.length; i++){ Integer floor = set.floor(nums[i] + t); Integer ceil = set.ceiling(nums[i] - t); if(floor != null && floor >= nums[i] || ceil != null && ceil <= nums[i]) return true; set.add(nums[i]); if(i >= k) set.remove(nums[i-k]); } return false; } }
相关文章推荐
- snailsvn mac上的versions
- 磁盘格式raid介绍
- TurboMail邮件系统与晶澳太阳能合作
- TurboMail邮件系统与晶澳太阳能合作
- travel to Hokkaido
- 哈理工大学ACM程序设计全国邀请赛D D Pairs(FFT)
- saltstack-----state.sls引用grains
- 11. Container With Most Water
- Socket.Available 属性
- Uva 10780 Again Prime? No Time.(分解质因子)
- wait_event_interruptible() 和 wake_up()的使用
- J2SE第八章——IO输出输出流( DataInputStream、DataOutputStream、序列化)
- 【VK Cup 2012】Codeforces 209C Trails and Glades
- 得到activiti中mainconfig
- AIDL进程间通信 Binder
- 70. Climbing Stairs*
- OpenLDAP main: TLS init def ctx failed: -1
- Inteil idea main函数和System.out.println()快捷键
- 浅谈机器学习
- 哈理工邀请赛 F.Fibonacci Again (矩阵快速幂)