-
Notifications
You must be signed in to change notification settings - Fork 1
/
15.三数之和.ts
60 lines (51 loc) · 1.07 KB
/
15.三数之和.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* @lc app=leetcode.cn id=15 lang=typescript
*
* [15] 三数之和
*/
// @lc code=start
function threeSum(nums: number[]): number[][] {
nums.sort();
if (nums.length < 3) {
return [];
}
const ans: number[][] = [];
const n = nums.length;
for (let i = 0; i < n; i++) {
const first = nums[i];
let left = i + 1,
right = n - 1;
if (first > 0) {
return ans;
}
if (i > 0 && first === nums[i - 1]) {
continue;
}
while (right > left) {
const sum = nums[i] + nums[left] + nums[right];
if (sum === 0) {
ans.push([nums[i], nums[left], nums[right]]);
while (right > left && nums[left] === nums[left + 1]) {
left++;
}
while (right > left && nums[right] === nums[right - 1]) {
right--;
}
left++;
right--;
} else if (sum > 0) {
right--;
} else {
left++;
}
}
}
return ans;
}
// @lc code=end
test("threeSum", () => {
expect(threeSum([-1, 0, 1, 2, -1, -4])).toEqual([
[-1, -1, 2],
[-1, 0, 1],
]);
});