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 I S at the most K.
1 classSolution {2 Public:3 BOOLContainsnearbyalmostduplicate (vector<int>& Nums,intKintt) {4Multiset <Long Long>BST;5 for(intI=0; I<nums.size (); i++)6 {7 if(Bst.size () ==k+1) Bst.erase (Bst.find (nums[i-k-1]));8Auto lb=Bst.lower_bound (Nums[i]);9 if(Lb!=bst.end () &&abs (*lb-nums[i]) <=t)return true;TenAuto ub=Bst.upper_bound (Nums[i]); One if(Ub!=bst.begin () &&abs (* (--ub)-nums[i]) <=t)return true; A Bst.insert (Nums[i]); - } - return false; the } -};
Lower_bound returns the ordinal of the minimum key value greater than or equal to N.
Upper_bound returns the ordinal +1 of the maximum key value that is less than N.
The position returned is a position that can be inserted directly.
Contains Duplicate III