-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.java
40 lines (32 loc) · 1.04 KB
/
16.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public int threeSumClosest(int[] nums, int target) {
int close = Integer.MAX_VALUE;
int ans = 0;
Arrays.sort(nums);
// 经典的一个循环+双指针。类似于三数之和,只不过多了大小的比较。注意j和i的流程控制
for (int k = 0; k < nums.length; k++) {
int i = k + 1;
int j = nums.length - 1;
while (i < j) {
int s = nums[i] + nums[j] + nums[k];
if (s == target) {
return s;
}
if (s > target) {
if (s - target < close) {
close=s-target;
ans = s;
}
j--;
} else {
if (target - s < close) {
close = target - s;
ans = s;
}
i++;
}
}
}
return ans;
}
}