Question:
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Solution:
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
if(nums.length<3||nums==null) return -1;
nums.sort((a,b) => a - b);
let closeNum = nums[0] + nums[1] + nums[2];
for (let i = 0; i < nums.length - 2; i++) {
let left = i + 1;
let right = nums.length - 1;
while (left < right) {
let sum = nums[left] + nums[right] + nums[i];
if (Math.abs(sum - target) < Math.abs(closeNum - target)) {
closeNum = sum;
}
if (sum < target) {
left ++;
} else {
right --;
}
}
}
return closeNum;
}
Runtime: 68 ms, faster than 75.96% of JavaScript online submissions for 3Sum Closest.