Leetcode 16 - 3Sum closest

Note:

  • Just like the other 3sum questions, just use double pointers.
  • Fix an i, then set left = i + 1, right = len - 1 and move either one by sum is bigger than target or not.
  • Keep compare with res while moving pointers.

Question:

Given an integer array nums of length n 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:

1
2
3
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
nums = nums.sort((a, b) => a - b);
let res = Infinity;
for (let i = 0; i < nums.length - 2; i++) {
let left = i + 1, right = nums.length - 1;
while (left < right) {
const sum = nums[i] + nums[left] + nums[right];
if (Math.abs(sum - target) < Math.abs(res - target)) res = sum;
if (sum < target) {
left++;
} else {
right--;
}
}
}
return res;
};