1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class N220 { public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { TreeSet<Integer> set=new TreeSet<Integer>(); for(int i=0;i<nums.length;i++){ Integer s=set.ceiling(nums[i]); if(s!=null&&s-t<=nums[i]){ return true;
} s=set.floor(nums[i]); if(s!=null&&nums[i]<=t+s){ return true;
} set.add(nums[i]); if(set.size()>k){ set.remove(nums[i-k]); }
}
return false;
}
public static void main(String[] args) { int[] nums={-1,2147483647};
System.out.println(containsNearbyAlmostDuplicate(nums,3,2147483647)); }
}
|