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