class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
minX = 10000
result = 0
for i in range(len(nums)-2):
left = i+1
right = len(nums)-1
if i == 0 or nums[i] > nums[i-1]:
while left < right:
temp = nums[i] + nums[left] + nums[right] - target
if temp == 0:
return target
elif abs(temp) < minX:
minX = abs(temp)
result = nums[i] + nums[left] + nums[right]
elif temp > 0:
right -= 1
elif temp < 0:
left += 1
return result
github项目地址:https://github.com/JockWang/LeetCode-Python