1 import sys 2 class Solution: 3 def threeSumClosest(self, nums: 'List[int]', target: int) -> int: 4 n = len(nums) 5 if n <= 3: 6 return sum(nums) 7 nums = sorted(nums) 8 mindiff = sys.maxsize 9 minsums = 0 10 for i in range(n-2): 11 j,k = i + 1,n - 1 12 while j < k: 13 sums = nums[i] + nums[j] + nums[k] 14 diff = sums - target 15 if diff == 0: 16 return target 17 elif diff < 0: 18 j += 1 19 else: 20 k -= 1 21 if abs(diff) < mindiff: 22 mindiff = abs(diff) 23 minsums = sums 24 return minsums