# 代码

/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const LENGTH=nums.length;
if(LENGTH<3) return [];
nums.sort((a,b)=>a-b);
let result = [],ai=nums[0],j=1,k=LENGTH-1,jksum=0,lastAi=undefined,lastAj=undefined,lastAk=undefined;
for (let i = 0; i < LENGTH - 2; lastAj=lastAk=undefined,++i,j=i+1,k=LENGTH-1,ai=nums[i]) {
if(lastAi===ai) continue;
while(j<k){
if(lastAj===nums[j]&&lastAk===nums[k]){j++,k--;continue;}
if((jksum=nums[j]+nums[k])===-ai){
lastAj=nums[j],lastAk=nums[k],lastAi=ai;
result.push([ai,nums[j],nums[k]]);
j++,k--;
}else{ jksum>-ai/*ai+aj+ak>0*/?k--:j++;}
}
}
return result;
};

//test cases

[
[-5, -5, -4, -4, -4, -2, -2, -2, 0, 0, 0, 1, 1, 3, 4, 4]
].forEach((v,i)=>{
console.log(for the ${i}th case${showArrayDeeply(v)}, your output is \${showArrayDeeply(threeSum(v))});
});

//helper method
function showArrayDeeply(arr){
var result=[];
(function show(arr,jar){
let subjar=[]
for(let ele of arr){
if(Array.isArray(ele)) show(ele,subjar);
else subjar.push(ele+"");
}
if(subjar.length>0) jar.push('['+subjar.join(',')+']');
})(arr,result);
return '['+result.join(',')+']';
};