Contains Duplicate III

Source: Internet
Author: User

    /** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.