-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum3Closest.java
More file actions
40 lines (38 loc) · 1.04 KB
/
Sum3Closest.java
File metadata and controls
40 lines (38 loc) · 1.04 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
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Arrays;
public class Closest3Sum {
public static int threeSumClosest(int[] nums, int target) {
if (nums.length < 4) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return sum;
}
Arrays.sort(nums);
int leftPos = 0;
int sumTarget = nums[0] + nums[1] + nums[2];
while (leftPos < nums.length - 2) {
int leftInd = leftPos + 1;
int rightInd = nums.length - 1;
while (leftInd < rightInd) {
int sum = nums[leftPos] + nums[leftInd] + nums[rightInd];
if (sum == target)
return target;
if (sum < target) {
if (Math.abs(sum - target) < Math.abs(sumTarget - target)) {
sumTarget = sum;
}
while(nums[leftInd] == nums[++leftInd] && leftInd < rightInd);
}
if (sum > target) {
if (Math.abs(sum - target) < Math.abs(sumTarget - target)) {
sumTarget = sum;
}
while(nums[rightInd] == nums[--rightInd] && leftInd < rightInd);
}
}
while(nums[leftPos] == nums[++leftPos] && leftPos < nums.length - 2);
}
return sumTarget;
}
}