forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (36 loc) · 1.05 KB
/
Solution.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
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = 0;
int n = nums.length;
int diff = Integer.MAX_VALUE;
for (int i = 0; i < n - 2; ++i) {
int t = twoSumClosest(nums, i + 1, n - 1, target - nums[i]);
if (Math.abs(nums[i] + t - target) < diff) {
res = nums[i] + t;
diff = Math.abs(nums[i] + t - target);
}
}
return res;
}
private int twoSumClosest(int[] nums, int start, int end, int target) {
int res = 0;
int diff = Integer.MAX_VALUE;
while (start < end) {
int val = nums[start] + nums[end];
if (val == target) {
return val;
}
if (Math.abs(val - target) < diff) {
res = val;
diff = Math.abs(val - target);
}
if (val < target) {
++start;
} else {
--end;
}
}
return res;
}
}