题目链接
public class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int lastDistance=Integer.MAX_VALUE;
int last=lastDistance;
int result=0;
out:for(int i=0;i<nums.length;i++)
{
if(last==nums[i])
{
continue;
}
last=nums[i];
int j=i+1,k=nums.length-1;
while(j<k)
{
int temp=(target-nums[i]-nums[j]-nums[k]);
if(Math.abs(temp)<lastDistance)
{
lastDistance=Math.abs(temp);
result=target-temp;
}
if(temp>0)
{
j++;
}
else if(temp<0)
{
k--;
}
else
{
break out;
}
}
}
return result;
}
}