forked from zzxboy1/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.js
34 lines (34 loc) · 788 Bytes
/
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
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort(function(a,b){
return a-b;
});
var target,result=[],len=nums.length;
for(var i=0;i<len;i++){
if (target===nums[i]){
continue;
}
else{
target=nums[i];
}
var low=i+1,high=len-1;
while(low<high){
if(nums[low]+nums[high]===-target){
result.push([target,nums[low],nums[high]]);
do{
low++;
}while(nums[low]===nums[low-1]);
}
else if(nums[low]+nums[high]<-target){
low++;
}
else{
high--;
}
}
}
return result;
};