Similar to 3Sum, we sort the array and use Two Pointers. Instead of looking for exactly 0, we track the sum with the minimum absolute difference from the target.
- Sort
nums. best_sum = float('inf').- For
iinrange(len(nums)):l = i + 1,r = len(nums) - 1.- While
l < r:curr_sum = nums[i] + nums[l] + nums[r].- If
abs(target - curr_sum) < abs(target - best_sum):best_sum = curr_sum. - If
curr_sum < target,l += 1. - Else if
curr_sum > target,r -= 1. - Else, return
target.
- Return
best_sum.
- Time Complexity: O(N^2).
- Space Complexity: O(log N) to O(N).
def three_sum_closest(nums, target):
nums.sort()
res = nums[0] + nums[1] + nums[2]
for i in range(len(nums)):
l, r = i + 1, len(nums) - 1
while l < r:
s = nums[i] + nums[l] + nums[r]
if abs(s - target) < abs(res - target):
res = s
if s > target:
r -= 1
elif s < target:
l += 1
else:
return res
return res