16. 3Sum Closest Posted on 2020-06-14 | Edited on 2021-01-22 | Views: LeetCode 1234567891011121314151617class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int result=nums[0]+nums[1]+nums[nums.length-1];; for(int i=0;i<nums.length-2;i++){ int start=i+1, end=nums.length-1; while(start<end){ int sum=nums[i]+nums[start]+nums[end]; if(sum>target) end--; else start++; result=Math.abs(sum-target)<Math.abs(result-target)?sum:result; } } return result; }}