# 最接近的三数之和

<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

```java
class Solution {
	int oneSumCloset(int[] nums, int i, int j, int start, int end, int target) {
		if (start == i || start == j)
			start = start + 1;
		if (end == i || end == j)
			end = end - 1;
		if (start == end) {
			return nums[start];
		} else if (end == start + 1 || end == start - 1) {
			if (Math.abs(nums[end] - target) > Math.abs(nums[start] - target)) {
				return nums[start];
			} else {
				return nums[end];
			}
		} else {
			int middle = (int) Math.floor((start + end) / 2);
			if (nums[middle] > target) {
				end = middle;
			} else {
				start = middle;
			}
			return oneSumCloset(nums, i, j, start, end, target);
		}
	}
	public int threeSumClosest(int[] nums, int target) {
		Arrays.sort(nums);
		int minValue = 0;
		boolean hasMin = false;
		for (int i = 0; i < nums.length - 2; i++) {
			for (int j = i + 1; j < nums.length - 1; j++) {
				int twoSum = nums[i] + nums[j];
				int rest = target - twoSum;
				int restClost = oneSumCloset(nums, i, j, j + 1, nums.length - 1, rest);
				int newValue = restClost + twoSum;
				;
				if (!hasMin) {
					minValue = newValue;
					hasMin = true;
				} else {
					int d1 = Math.abs(minValue - target);
					int d2 = Math.abs(newValue - target);
					if (d1 > d2) {
						minValue = newValue;
					}
				}
			}
		}
		return minValue;
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```