forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasentino.ts
More file actions
34 lines (34 loc) · 962 Bytes
/
casentino.ts
File metadata and controls
34 lines (34 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function threeSum(nums: number[]): number[][] {
const sortedNums = nums.sort((a, b) => a - b);
const results: number[][] = [];
for (let i = 0; i < sortedNums.length - 2; i++) {
const current = sortedNums[i];
if (current > 0) {
return results;
}
if (i > 0 && current === sortedNums[i - 1]) {
continue;
}
let left = i + 1;
let right = sortedNums.length - 1;
while (left < right) {
const sum = current + sortedNums[left] + sortedNums[right];
if (sum > 0) {
right -= 1;
} else if (sum < 0) {
left += 1;
} else {
results.push([current, sortedNums[left], sortedNums[right]]);
left += 1;
right -= 1;
while (left < right && sortedNums[left] === sortedNums[left - 1]) {
left += 1;
}
while (left < right && sortedNums[right] === sortedNums[right + 1]) {
right -= 1;
}
}
}
}
return results;
}