g0001_0100.s0016_3sum_closest.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0016_3sum_closest;
// #Medium #Array #Sorting #Two_Pointers #Level_2_Day_14_Sliding_Window/Two_Pointer
// #2023_08_09_Time_3_ms_(99.88%)_Space_42.6_MB_(97.42%)
import java.util.Arrays;
/**
* 16 - 3Sum Closest\.
*
* Medium
*
* Given an integer array `nums` of length `n` and an integer `target`, find three integers in `nums` such that the sum is closest to `target`.
*
* 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).
*
* **Example 2:**
*
* **Input:** nums = [0,0,0], target = 1
*
* **Output:** 0
*
* **Constraints:**
*
* * `3 <= nums.length <= 1000`
* * `-1000 <= nums[i] <= 1000`
* * -104 <= target <= 104
**/
public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3) {
return 0;
}
if (nums.length == 3) {
return nums[0] + nums[1] + nums[2];
}
Arrays.sort(nums);
int n = nums.length;
int sum = nums[0] + nums[1] + nums[2];
for (int i = 0; i < n - 2; i++) {
if (nums[i] + nums[n - 1] + nums[n - 2] < target) {
sum = nums[i] + nums[n - 1] + nums[n - 2];
continue;
}
if (nums[i] + nums[i + 1] + nums[i + 2] > target) {
int temp = nums[i] + nums[i + 1] + nums[i + 2];
return lessGap(sum, temp, target);
}
int j = i + 1;
int k = n - 1;
while (j < k) {
int temp = nums[i] + nums[j] + nums[k];
if (temp == target) {
return target;
}
if (temp < target) {
j++;
} else {
k--;
}
sum = lessGap(sum, temp, target);
}
}
return sum;
}
private int lessGap(int sum, int temp, int target) {
return Math.abs(sum - target) < Math.abs(temp - target) ? sum : temp;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy