Two Sum - Closest
Given an array nums
of n _integers, find two integers in_nums_such that the sum is closest to a given number,_target.
Return the difference between the sum of the two integers and the target.
Example
Given arraynums
=[-1, 2, 1, -4]
, and target =4
.
The minimum difference is1
. (4 - (2 + 1) = 1).
Note
左边是越走越大的,右边是越走越小的。
相加小的话,想往右走,left加
相加大的话,想往左走,right减
维护一个最小的diff
Code
Last updated