Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
public class Solution { public int threeSumClosest(int[] nums, int target) { //本题是3Sum的变形,找出最接近target的值 //跟3Sum唯一的不同是,需要增加一个变量min(注意初始化值),内层每次循环时,需要与它进行比较。保留住距离target最小的值 //判断重复的逻辑可有可无,因为本题的输出只是需要返回总结果 Arrays.sort(nums); int res=0; int min=Integer.MAX_VALUE; for(int i=0;i0&&nums[i]==nums[i-1])continue; int right=nums.length-1; while(left target){ if(temp-target