*
* @param num int整型一维数组
* @return int整型二维数组
*/
function threeSum( num ) {
// write code here
let len = num.length;
if(len < 3) return [];
num.sort(function(a,b){return a - b}); // 排序
let res = [];
let target,left,right,sum;
for(let i = 0;i < len - 2;i++){
while(i > 0 && num[i] === num[i - 1]) i++; //去重
target = -num[i];
left = i + 1;
right = len - 1;
while(left < right){
sum = num[left] + num[right];
if(sum === target){
res.push([num[i],num[left],num[right]]);
while(left < right && num[left] === num[left + 1]) left++; //去重
left++;
}else{
sum > target ? right-- : left++;
}
}
}
return res;
}
module.exports = {
threeSum : threeSum
};