-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution016.java
More file actions
27 lines (25 loc) · 1 KB
/
Solution016.java
File metadata and controls
27 lines (25 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Solution{
public int threeSumClosest(int[] nums, int target){
int closest_target = nums[0] + nums[1] + nums[2]; // closest_target为最接近的3Sum
int smallest_difference_value = Math.abs(closest_target - target); // 3Sum与target之间最小的差值
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int low = i + 1;
int high = nums.length - 1;
while(low < high){
int sum = nums[i] + nums[low] + nums[high];
int temp = Math.abs(sum - target);
if (temp < smallest_difference_value) {
closest_target = sum;
smallest_difference_value = temp;
}
// sum的值必须无限接近target,以此来判断low和high指针的移动
if (sum < target) {
low++;
}else
high--;
}
}
return closest_target;
}
}