public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return -1;
}
int min = Integer.MAX_VALUE;
int result = 0;
Arrays.sort(nums);
for (int i = 0; i <= nums.length - 3; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
int diff = Math.abs(sum - target);
if (diff == 0) {
return target;
}
if (diff < min) {
min = diff;
result = sum;
}
if (sum < target) {
left++;
} else {
right--;
}
}
}
return result;
}
}