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 | import java.util.SortedSet; import java.util.TreeSet; public class Solution_220 { public static boolean containsNearbyAlmostDuplicate( int [] nums, int k, int t) { if (nums == null || k < 0 || t < 0 || nums.length < 2 ){ return false ; } SortedSet<Integer> set = new TreeSet<>(); for ( int i = 0 ; i < nums.length; i++) { SortedSet<Integer> subset = set.subSet(nums[i]-t, nums[i]+t+ 1 ); if (!subset.isEmpty()){ return true ; } if (i > k - 1 ){ set.remove(nums[i-k]); } set.add(nums[i]); } return false ; } public static void main(String[] args) { int [] nums = { 1 , 2 , 3 , 1 }; System.out.println(containsNearbyAlmostDuplicate(nums, 4 , 0 )); } } |
自已验证没有问题,提交后一直报错,请老师看看。