-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreeSumClosest.java
37 lines (34 loc) · 1007 Bytes
/
ThreeSumClosest.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
import java.util.Arrays;
/**
* @author LBW
*/
public class ThreeSumClosest {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int len = nums.length;
int result = nums[0] + nums[1] + nums[2];
for (int i = 0; i < len; i++) {
if (i > 0 && nums[i] == nums[i-1])
continue;
int nextTarget = target - nums[i];
int left = i + 1;
int right = len - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (Math.abs(sum - nextTarget) < Math.abs(result - target)) {
result = nums[i] + sum;
}
if (sum == nextTarget) {
return result;
}
else if (sum < nextTarget) {
left++;
}
else {
right--;
}
}
}
return result;
}
}