var threeSumClosest = function (nums, target) { // 先将nums 按升序排序 nums.sort((a, b) => a - b); const len = nums.length; //初始化一个最小值 let res = Number.MAX_SAFE_INTEGER; for (let i = 0; i < len; i++) { // 定义左右指针 let left = i + 1; let right = len - 1; while (left < right) { //计算出当前的三数之合 const sum = nums[i] + nums[left] + nums[right]; //更新最小值 if (Math.abs(sum - target) < Math.abs(res - target)) { res = sum; } //根据最小值和target的关系移动左右指针 if (sum < target) { left++; } else { right--; } } } return res; };