L553 Two Sum Closest
Given an arraynums
ofn_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.
Given arraynums
=[-1, 2, 1, -4]
, andtarget=4
.
The minimum difference is1
. (4 - (2 + 1) = 1).
Do it in O(nlogn) time complexity.
还是2 pointer,先排序,然后还得用变量keep track of 全局min。这题因为是求closest,所以不能hashmap。
Last updated