-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathres.js
55 lines (46 loc) · 945 Bytes
/
res.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
49
50
51
52
53
54
55
/**
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-04-03 23:45:47
*
* @param {number[]} nums
* @return {number[][]}
*/
let threeSum = function(nums) {
nums.sort(function(a, b) {
return a - b;
});
let numlen = nums.length,
end = nums[numlen - 1],
eInd = numlen - 2,
res = [];
if (numlen < 3 || nums[0] > 0 || end < 0) {
return [];
}
for (let i = 0; i < eInd; i++) {
let revi = -nums[i],
j = i + 1,
k = numlen - 1;
if (revi < 0) {
break;
}
while (j < k) {
let norj = nums[j],
nork = nums[k];
if (norj + nork > revi) {
k--;
} else if (norj + nork < revi) {
j++;
} else {
res.push([nums[i], nums[j], nums[k]]);
// forbid duplicated numbers
while (j + 1 < numlen && nums[j] === nums[j + 1]) j++;
while (nums[k] === nums[k - 1]) k--;
k--;
j++;
}
}
while (i + 1 < numlen && nums[i] === nums[i + 1]) i++;
}
return res;
};