/**
 *
 * @param num int整型一维数组
 * @return int整型二维数组
 */
function threeSum(num) {
    // write code here
    num = num.sort((a, b) => a - b); // 整体升序
    let len = num.length;
    let arr = [];
    if (num.length < 3) {
        return [];
    }
    for (let i = 0; i < len-2; i++) {
        if(i!=0&&num[i]==num[i-1]){//防止重复
            continue;
        }
        let j = i + 1;
        let z = len - 1;
       let total ;
        while (j < z) {
             total = num[i] + num[j] + num[z];
            if (total === 0) {
                arr.push([num[i], num[j], num[z]]);
                while (j < z - 1 && num[j] === num[j + 1]) {
                    //去重
                    j++;
                }
                while (z - 1 > j && num[z] === num[z - 1]) {
                    //去重
                    z--;
                }
                j++;
                z--
            }else if(total>0){
                z--
            }else{
                j++
            }
        }
        
    }
    return arr;
}
module.exports = {
    threeSum: threeSum,
};