closest
to infinity.nums
.i
from 0 to n - 3
:
left
to i + 1
and right
to n - 1
.left
is less than right
:
currentSum
as nums[i] + nums[left] + nums[right]
.currentSum
is equal to target
, return currentSum
immediately as it is the closest possible sum.currentSum
and target
is less than the absolute difference between closest
and target
, update closest
to currentSum
.currentSum
is less than target
, increment left
.right
.closest
after the loop ends.