-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.3-sum.js
65 lines (56 loc) · 1.45 KB
/
15.3-sum.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
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode id=15 lang=javascript
*
* [15] 3Sum
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
if (nums.length < 3) {
return []
}
cmp = (x, y) => x - y
nums = nums.sort(cmp)
let left, right;
let res = []
for (let i = 0; i < nums.length; i++) {
if (nums[i] == nums[i - 1] || nums[i] > 0)
continue
left = i + 1
right = nums.length - 1
if (nums[left] > -nums[i] || nums[right]*2 < -nums[i])
continue
while (left < right) {
if (nums[left] == nums[left - 1]) {
left ++
continue
}
if (nums[right] == nums[right + 1]) {
right --
continue
}
sum = nums[left] + nums[right]
if (sum > -nums[i]) {
right--
} else if (sum < -nums[i]) {
left++
} else{
res.push([nums[i], nums[left], nums[right]])
left++
}
}
}
return res
};
// @lc code=end
console.log(threeSum([-1,0,1,2,-1,-4])) //[[-1,-1,2],[-1,0,1]]
console.log(threeSum([]))
console.log(threeSum([-1,-1, -1, -1,0,1,2,-1,-4]))
console.log(threeSum([0,0,0,0,0]))
console.log(threeSum([1,1,1,1,0]))
console.log(threeSum([-2,0,1,1,2]))
console.log(threeSum([3,0,-2,-1,1,2]))
console.log(threeSum([1,1,-2]))