class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ nums.sort() res = sum(nums[:3]) for i in range(len(nums) - 2): l = i + 1 r = len(nums) - 1 while l < r: three = nums[i] + nums[l] + nums[r] if abs(three - target) < abs(res - target): res = three if three > target: r -= 1 elif three < target: l += 1 else: return res return res