-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.js
More file actions
26 lines (25 loc) · 926 Bytes
/
threeSum.js
File metadata and controls
26 lines (25 loc) · 926 Bytes
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
var threeSum2 = function(nums) {
let res = [];
nums.sort((a,b) => a - b)
for(let i=0;i < nums.length-2;i++){
if( i === 0 || nums[i] !== nums[i-1]){
let low = i+1;
let high = nums.length-1;
let sum = 0;
console.log(nums[low] + nums[high] + nums[i],nums[low] , nums[high] , nums[i]);
while(low < high){
if(nums[low] + nums[high] + nums[i] == 0 ){
res.push([nums[low],nums[high],nums[i]]);
while (low < high && nums[low] === nums[low+1]) low++;
while (low < high && nums[high] === nums[high-1]) high--;
low++;
high--;
}else if(nums[low] + nums[high] + nums[i] > 0) {
high--;
}else low++;
}
}
}
return res;
};
console.log(threeSum2([1,-1,-1,0]))