最接近的三数之和
class Solution:
def threeSumClosest(self, nums, target):
ret = float('inf')
nums.sort()
length = len(nums)
for i in range(length - 2):
left = i + 1
right = length - 1
while left < right:
tmp = nums[i] + nums[left] + nums[right]
ret = tmp if abs(tmp - target) < abs(ret - target) else ret
if tmp == target:
return target
最接近的三数之和最先出现在Python成神之路。
共有 0 条评论