-
Notifications
You must be signed in to change notification settings - Fork 65
/
三数之和.js
32 lines (32 loc) · 807 Bytes
/
三数之和.js
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
/**
* @param {number[]} nums
* @return {number[][]}
15. 三数之和
*/
var threeSum = function(nums) {
nums.sort((x,y) => x-y);
const n = nums.length;
const ans = [];
for(let i=0; i<n; i++) {
let val = nums[i];
if(val > 0) continue;
if(i>0 && val === nums[i-1]) continue;
let l = i + 1;
let r = n - 1;
while(l < r) {
let sum = val + nums[l] + nums[r];
if (sum === 0) {
ans.push([val, nums[l], nums[r]])
while(l<r && nums[l] === nums[l+1]) l++;
while(l<r && nums[r] === nums[r+1]) i--;
l++;
r--;
}else if(sum>0) {
r--;
}else{
l++;
}
}
}
return ans;
};