/**
*
* @param num int整型一维数组
* @return int整型二维数组
*/
function threeSum( num ) {
// write code here
let result = []
let len = num.length
num.sort(function(a,b){return a-b});
for(let i=0;i<len-2;i++){
let head = i+1
let tail = len-1
while(head<tail){
let sum = num[i]+num[head]+num[tail]
if(sum<0) head++
else if(sum>0) tail--
else{
result.push([num[i],num[head],num[tail]])
while(head+1<tail && num[head+1] == num[head]) head++
while(tail-1>head && num[tail-1] == num[tail]) tail--
head++
tail--
}
}
while(i<len-2 && num[i+1] == num[i]) i++
}
return result
}
module.exports = {
threeSum : threeSum
}; 
京公网安备 11010502036488号