答案:
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int c = Math.abs(nums[0] + nums[1] + nums[2] - target);
int sum = nums[0] + nums[1] + nums[2];
for(int i=0;i<nums.length;i++){
for(int j=i+1,k=nums.length-1;j<k;){
int tempc = nums[i] + nums[j] + nums[k] - target;
if(tempc<c){
c = Math.abs(tempc);
sum = nums[i]+nums[j]+nums[k];}
if(tempc==0){
return target;
}else if(tempc<0){
j++;
}else{
k--;
}
}
}
return sum;
}
}