[Leetcode][016] 3Sum Closest (Java)

时间:2023-03-08 16:38:01
[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/

【标签】Array; Two Pointers

【个人分析】

  这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum

 public class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = target;
int len = nums.length;
if (len < 3) {
return 0;
}
Arrays.sort(nums);
for (int i = 0; i < len; i++) {
int number = nums[i]; int leftIndex = i + 1;
int rightIndex = len - 1;
while (leftIndex < rightIndex) {
int threeSum = number + nums[leftIndex] + nums[rightIndex];
if (threeSum == target) {
// best result found!
return target;
} else {
// update global result
if ( result == target ||
Math.abs(target - threeSum) < Math.abs(target - result)) {
result = threeSum;
}
if (threeSum < target) {
while (leftIndex < rightIndex &&
nums[leftIndex] == nums[leftIndex + 1]) {
leftIndex++;
}
leftIndex++;
} else {
while (leftIndex < rightIndex &&
nums[rightIndex] == nums[rightIndex - 1]) {
rightIndex--;
}
rightIndex--;
}
}
}
}
return result;
} }