classSolution(object):defthreeSumClosest(self, nums, target):"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
res = nums[0]+nums[1]+nums[2]
re_min = res - target
for k inrange(len(nums)-2):if k>0and nums[k]==nums[k-1]:continue
i = k+1
j =len(nums)-1while i<j:
x = nums[k]+nums[i]+nums[j]
s = x - target
ifabs(s)<abs(re_min):
re_min = s
res = x
if s==0:return target
elif s<0:
i +=1while i<j and nums[i]==nums[i-1]:
i +=1else:
j -=1while i<j and nums[j]==nums[j+1]:
j -=1return res