/** 220. Contains Duplicate III * 1.2 by Mingyang * TREESET data structure (JAVA) is implemented using red-black trees and is a kind of balanced binary tree. * This data structure supports the following actions: * 1. The floor () method returns the largest element in the set that ≤ the given element, or null if no such element exists. * 2. The ceiling () method returns the smallest element in the set ≥ the given element, or null if no such element exists. */ Public Static BooleanContainsnearbyalmostduplicate (int[] Nums,intKintt) {if(K < 1 | | T < 0) return false; TreeSet<Integer> set =NewTreeset<integer>(); for(inti = 0; i < nums.length; i++){ intn =Nums[i]; if(Set.floor (n)! =NULL&& N <= t + set.floor (n) | | Set.ceiling (n)! =NULL&& set.ceiling (n) <= T +N)return true; //There is only a size to judge, so we have to introduce more of a position concept inside, so we need to control our length in K, using the idea of sliding windowsSet.add (n); if(I >= k)//also has the idea of sliding windows inside!!! As long as the boundary is exceeded, the bottom boundary is automatically canceled .Set.remove (Nums[i-K]); } return false; }
Contains Duplicate III