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:
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).
Example 2:
Input: nums = [0,0,0], target = 1 Output: 0 Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
Constraints:
3 <= nums.length <= 500
-1000 <= nums[i] <= 1000
-104 <= target <= 104
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
def twoSumClosest(nums, start, end, target):
res = 0
diff = 10000
while start < end:
val = nums[start] + nums[end]
if val == target:
return val
if abs(val - target) < diff:
res = val
diff = abs(val - target)
if val < target:
start += 1
else:
end -= 1
return res
nums.sort()
res, n = 0, len(nums)
diff = 10000
for i in range(n - 2):
t = twoSumClosest(nums, i + 1, n - 1, target - nums[i])
if abs(nums[i] + t - target) < diff:
res = nums[i] + t
diff = abs(nums[i] + t - target)
return res
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = 0;
int n = nums.length;
int diff = Integer.MAX_VALUE;
for (int i = 0; i < n - 2; ++i) {
int t = twoSumClosest(nums, i + 1, n - 1, target - nums[i]);
if (Math.abs(nums[i] + t - target) < diff) {
res = nums[i] + t;
diff = Math.abs(nums[i] + t - target);
}
}
return res;
}
private int twoSumClosest(int[] nums, int start, int end, int target) {
int res = 0;
int diff = Integer.MAX_VALUE;
while (start < end) {
int val = nums[start] + nums[end];
if (val == target) {
return val;
}
if (Math.abs(val - target) < diff) {
res = val;
diff = Math.abs(val - target);
}
if (val < target) {
++start;
} else {
--end;
}
}
return res;
}
}
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function (nums, target) {
let len = nums.length;
nums.sort((a, b) => a - b);
let diff = Infinity;
let res;
for (let i = 0; i < len - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1,
right = len - 1;
let cur = nums[i] + nums[i + 1] + nums[i + 2];
if (cur > target) {
let newDiff = Math.abs(cur - target);
if (newDiff < diff) {
diff = newDiff;
res = cur;
}
break;
}
while (left < right) {
cur = nums[i] + nums[left] + nums[right];
if (cur === target) return target;
let newDiff = Math.abs(cur - target);
if (newDiff < diff) {
diff = newDiff;
res = cur;
}
if (cur < target) {
while (nums[left] === nums[left + 1]) left++;
left++;
continue;
} else {
while (nums[right] === nums[right - 1]) right--;
right--;
continue;
}
}
}
return res;
};