-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.js
34 lines (30 loc) · 836 Bytes
/
3sum.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
33
34
var threeSum = function (nums) {
nums = nums.sort();
let l = nums.length;
let ans = [];
for (let i = 0; i < l; i++) {
if (i > 0 && nums[i]== nums[i - 1]) {
continue;
}
let start = i+1;
let end = l - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] > 0) {
end--;
}
else if (nums[i] + nums[start] + nums[end] < 0) {
start++;
}
else {
ans.push([nums[i], nums[start], nums[end]]);
start = start + 1;
while (start<end && nums[start]==nums[start-1]) {
start = start + 1;
}
}
}
}
return ans;
};
var nums = [-1, 0, 1, 0];
console.log(threeSum(nums));