# My Java Solution

• ``````public class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
if(nums.length < 3) return 0;
int result = nums[0] + nums[1] + nums[2] - target;

for(int i = 0; i < nums.length - 2; i++){
int new_target = target - nums[i];
int point1 = i + 1;
int point2 = nums.length - 1;
if(i > 0 && nums[i-1] == nums[i]) continue;
if(nums[point1+1] + nums[point1] >= new_target){
if(Math.abs(nums[point1+1] + nums[point1] - new_target) < Math.abs(result))
result = nums[point1+1] + nums[point1] - new_target;
}else if(nums[point2-1] + nums[point2] <= new_target){
if(Math.abs(nums[point2-1] + nums[point2] - new_target) < Math.abs(result))
result = nums[point2-1] + nums[point2] - new_target;
}else{
while(point1 < point2){
int total = nums[point1] + nums[point2];
if(total < new_target){
point1 ++;
}else if(total > new_target){
point2 --;
}else{ return target;}
if(Math.abs(total - new_target) < Math.abs(result)) result = total - new_target;
}
}

}
return result + target;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.