forked from EasyHard/leetcodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3sum.js
48 lines (46 loc) · 1 KB
/
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
var DEBUG = process.env.DEBUG;
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums = nums.sort(function (a, b) {
return a - b;
});
var ans = [];
for (var i = 0; i < nums.length;) {
var fixed = nums[i];
var j = i + 1; var k = nums.length - 1;
while (j < nums.length && k >= 0 && j < k) {
var curr = fixed + nums[j] + nums[k];
if (curr === 0) {
ans.push([fixed, nums[j], nums[k]]);
do {
j++;
} while (j < nums.length && nums[j-1] === nums[j])
do {
k--;
} while (k >= 0 && nums[k+1] === nums[k])
} else if (curr < 0) {
j++;
} else {
k--;
}
}
do {
i++;
} while (i < nums.length && nums[i] == nums[i-1]);
}
return ans;
};
if (DEBUG) {
[
[],
[-1,0,1,2,-1,-4],
[1, 2, -1],
[-1, -1, -1, -1, -1, -1, 2, 0, 0],
[-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]
].forEach(function (input) {
console.log(threeSum(input));
});
}