Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Examples:
**Input:**Given array nums = [-1, 2, 1, -4], and target = 1. **Output:**The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
classSolution: defthreeSumClosest(self, nums: List[int], target: int) -> int: nums.sort() n = len(nums) res = float("inf") for i in range(n): if i > 0and nums[i] == nums[i-1]: continue left = i + 1 right = n - 1 while left < right : #print(left,right) cur = nums[i] + nums[left] + nums[right] if cur == target:return target if abs(res-target) > abs(cur-target): res = cur if cur > target: right -= 1 elif cur < target: left += 1 return res