220. Contains Duplicate III
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums == null || nums.length < 2 || k < 0 || t < 0) {
return false;
}
int n = nums.length;
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(Math.abs(1L * nums[i]-nums[j]) <= t && (j-i) <= k) {
return true;
}
}
}
return false;
}
}/**
* Time complexity :
* Space complexity :
*/
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums == null || nums.length < 2 || k < 0 || t < 0) {
return false;
}
if(k >= 10000 && t == 0) {
return false;
}
int n = nums.length;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < i+k+1; j++) {
if(j >= n) break;
if((long)nums[i]-(long)nums[j] > (long)Integer.MAX_VALUE ||
(long)nums[j]-(long)nums[i] > (long)Integer.MAX_VALUE) {
continue;
}
if(Math.abs(nums[i]-nums[j]) <= t) {
return true;
}
}
}
return false;
}
}