forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-3Sum.js
48 lines (36 loc) · 1.31 KB
/
15-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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* https://leetcode.com/problems/3sum/
* Time O(N ^ 2) | Space O(N)
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums, sums = []) {
nums.sort((a, b) => a - b);
for (let first = 0; first < nums.length - 2; first++) {
if (isPrevDuplicate(nums, first)) continue;
const [ target, left, right ] = [ (-nums[first]), (first + 1), (nums.length - 1) ];
search(nums, target, left, right, sums);
}
return sums;
};
const isPrevDuplicate = (nums, index) => nums[index - 1] === nums[index];
const isNextDuplicate = (nums, index) => nums[index] === nums[index + 1];
const search = (nums, target, left, right, sums) => {
while (left < right) {
const [ leftVal, rightVal ] = [ nums[left], nums[right] ];
const sum = leftVal + rightVal;
const isTarget = sum === target;
if (isTarget) {
sums.push([ -target, leftVal, rightVal ]);
left++;
right--;
while (left < right && isPrevDuplicate(nums, left)) left++;
while (left < right && isNextDuplicate(nums, right)) right--;
continue;
}
const isTargetGreater = sum < target;
if (isTargetGreater) left++;
const isTargetLess = target < sum;
if (isTargetLess) right--;
}
};