forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.ts
More file actions
23 lines (20 loc) · 685 Bytes
/
res.ts
File metadata and controls
23 lines (20 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function containsNearbyAlmostDuplicate(nums: number[], k: number, t: number): boolean {
if (nums.length < 2) {
return false;
}
const numsList = nums.map((num, index) => ({
val: num,
index,
}));
numsList.sort((a, b) => a.val - b.val);
for (let i = 0; i < numsList.length-1; i++) {
for (let j = i+1; j < numsList.length; j++) {
if (Math.abs(numsList[j].val - numsList[i].val) > t) {
break;
} else if (Math.abs(numsList[j].val - numsList[i].val) <= t && Math.abs(numsList[j].index - numsList[i].index) <= k) {
return true;
}
}
}
return false;
};