class Solution: def threeSumClosest(self, nums, target: int) -> int: if len(nums)==3: return nums[0]+nums[1]+nums[2] nums.sort() mins=float('inf') res=target for i in range(len(nums)-2): #print('i= '+str(i)) left,right=i+1,len(nums)-1 while left<right: sums=nums[i]+nums[left]+nums[right] if sums==target: return target elif sums<target: if target-sums<mins: mins=target-sums res=sums left+=1 while left+1<right and nums[left]==nums[left-1]: left+=1 else: if sums-target<mins: mins=sums-target res=sums right-=1 while right-1>left and nums[right]==nums[right+1]: right-=1 return res
——2019.10.7