public int threeSumClosest(int[] nums , int target) {
int res = 0;
int tmp = 0;
int lastspace = 0;
boolean flag = true;
Arrays.sort(nums);
for(int i = 0 ; i < nums.length ; i++){
int j = i + 1;
int z = nums.length -1;
while(j < z){
tmp = nums[i] + nums[j] + nums[z];
if(Math.abs(tmp - target) < lastspace || (lastspace == 0 && flag)){
lastspace = Math.abs(tmp - target);
res = tmp;
if(tmp - target < 0){
j++;
}else{
z--;
}
flag = false;
}else{
if(lastspace + target <= nums[i] + nums[j + 1] + nums[z]){
z--;
}else{
j++;
}
}
}
}
return res;
}