# 3 Sum Closest

题目：

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.

You may assume that each input would have exactly one solution.

分析：

双指针，不断更新最小diff.不知道为什么不去重。

解法：

```
    public int threeSumClosest(int[] nums, int target) {
        int sum = 0, minDiff = Integer.MAX_VALUE;
        Arrays.sort(nums);
        for(int j = 0; j < nums.length - 2; j++){

                int first = nums[j], start = j + 1, end = nums.length - 1;            
                while(start < end){
                    int second = nums[start], third = nums[end];
                    int localSum = first + second + third;
                    int diff = Math.abs(target - localSum);
                    if(diff < minDiff){
                        sum = localSum;
                        minDiff = diff;
                    }
                    if(localSum < target){
                        start ++;                       
                    }else{
                        end --;
                    }                               
                }

        }
        return sum;        
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/high-frequency/3-sum-closest.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
