-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-3sum.js
43 lines (43 loc) · 1.07 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
/**
* For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
let res = [];
nums.sort((a,b)=>{ //sort()函数默认字典序排序,比如[-4,-2,2,1].sort() = [-2,-4,1,2]
return a-b;
});
for(let i = 0 ;i < nums.length ; i++){
if(nums[i]>0)
break;
if(i > 0 && nums[i] == nums[i-1])
continue;
let aim = 0 - nums[i];
let j = i+1;
let k = nums.length - 1;
while (j < k) {
let sum = nums[j] + nums[k];
if( sum === aim){
while(nums[j+1] === nums[j] && nums[k] === nums[k-1] && j<k){
j++;
k--;
}
res.push([nums[i],nums[j++],nums[k--]]);
}else if(sum < aim){
j++;
}else {
k--;
}
}
}
return res;
};
console.log(threeSum([-2,0,3,-1,4,0,3,4,1,1,1,-3,-5,4,0]));