Java for Leetcode Contains Duplicate III

Source: Internet
Author: User

Given an array of integers, find out whether there is II distinct indices i and J in the array such th At the difference between Nums[i] and Nums[j] are at most T and the difference between i and J are at the most K.

Problem Solving Ideas:

Violence enumeration will be LTE, the problem-solving idea is to solve with SortedSet, SortedSet can return a subset in a certain range, at the same time to determine whether this subset is empty can be resolved, the main problem needs to use a long type!

The Java implementation is as follows:

Import java.util.*;p Ublic class Solution {public    Boolean containsnearbyalmostduplicate (int[] nums, int k, int t) {
   if (nums==null | | nums.length<2| | k<1 | | T<0)         return false;          sortedset<long> set = new Treeset<long> ();          for (int j=0; j<nums.length; J + +) {              sortedset<long> subset =  Set.subset ((Long) nums[j]-t, (long) nums[j ]+T+1);              if (!subset.isempty ())             return true;              if (j>=k)                 Set.remove ((long) nums[j-k]);               Set.add ((long) nums[j]);               }          return false;      }}

Java for Leetcode 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.