220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indicesiandjin the array such that the difference betweennums[i]andnums[j]is at mosttand the difference betweeniandjis at mostk.
维护一个windon
treeset:
null
if there is no such element.
floor(Ee)
null
if there is no such element.
remove(Objecto)
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; } }优质内容筛选与推荐>>