Given an array
nums
of n integers and an integertarget
, find three integers innums
such that the sum is closest totarget
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example 1:
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Thoughts
- Same solution like 3 Sum
- Store a closet var to compare and get the closet value
Code(JAVA)
public int threeSumClosest(int[] nums, int target) {
int res = Integer.MAX_VALUE;
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++){
int l = i +1, r = nums.length - 1;
while(l < r) {
int val = nums[i] + nums[l] + nums[r];
if(res == Integer.MAX_VALUE ||
Math.abs(target - val) < Math.abs(target - res))
res = val;
if(val == target)
return val;
else if(val < target)
l ++;
else
r --;
}
}
return res;
}