# 最接近的三数之和

<p>给定一个包括&nbsp;<em>n</em> 个整数的数组&nbsp;<code>nums</code><em>&nbsp;</em>和 一个目标值&nbsp;<code>target</code>。找出&nbsp;<code>nums</code><em>&nbsp;</em>中的三个整数，使得它们的和与&nbsp;<code>target</code>&nbsp;最接近。返回这三个数的和。假定每组输入只存在唯一答案。</p><p>&nbsp;</p><p><strong>示例：</strong></p><pre><strong>输入：</strong>nums = [-1,2,1,-4], target = 1<strong><br />输出：</strong>2<strong><br />解释：</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。</pre><p>&nbsp;</p><p><strong>提示：</strong></p><ul>	<li><code>3 &lt;= nums.length &lt;= 10^3</code></li>	<li><code>-10^3&nbsp;&lt;= nums[i]&nbsp;&lt;= 10^3</code></li>	<li><code>-10^4&nbsp;&lt;= target&nbsp;&lt;= 10^4</code></li></ul>

## template

```python
from typing import List
class Solution:
	def threeSumClosest(self, nums: List[int], target: int) -> int:
		nums.sort()
		cur = 0
		closest = nums[0] + nums[1] + nums[2]
		while cur < len(nums) - 2:
			left = cur + 1
			right = len(nums) - 1
			while left < right:
				n = nums[cur] + nums[left] + nums[right]
				if abs(target - n) < abs(target - closest):
					closest = n
				if n == target:
					break
				elif n > target:
					t = right - 1
					while (t > left and nums[t] == nums[right]):
						t -= 1
					right = t
				else:
					t = left + 1
					while (t < right and nums[t] == nums[left]):
						t += 1
					left = t
			t = cur + 1
			while (t < len(nums) and nums[t] == nums[cur]):
				t += 1
			cur = t
		return closest
# %%
s = Solution()
print(s.threeSumClosest(nums = [-1,2,1,-4], target = 1))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```