Leetcode16.最接近的三数之和
class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int len = nums.length; int re = nums[0] + nums[1] + nums[2]; for (int i = 0; i < nums.length - 2; i++) { int start = i + 1; int end = len - 1; while(start < end){ int temp = nums[i] + nums[start] + nums[end]; if(Math.abs(temp - target) < Math.abs(re - target)){ re = temp; } if(temp > target){ end--; } else if(temp < target){ start++; } else{ return target; } } } return re; } }排序加双指针。