-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSumClosest.java
More file actions
57 lines (48 loc) · 1.59 KB
/
Copy pathThreeSumClosest.java
File metadata and controls
57 lines (48 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Arrays;
public class ThreeSumClosest {
public static void main(String[] args) {
// TODO Auto-generated method stub
threeSumClosest(new int[] {0, 2, 1, -3}, 0);
threeSumClosest(new int[] {1, 1, 1, 0}, 100);
}
public static int threeSumClosest(int[] nums, int target) {
// sort the array to make skipping duplicates easier
Arrays.sort(nums);
if (nums.length < 3) {
return target;
}
int closest = nums[0] + nums[1] + nums[2];
// for each element
for (int i = 0; i < nums.length; i++) {
// skip duplicates
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
int low = i + 1;
int high = nums.length - 1;
// search from both sides
while (low < high) {
int currSum = nums[high] + nums[low] + nums[i];
int distance = (nums[high] + nums[low] + nums[i]) - target;
if (Math.abs(distance) < Math.abs(closest - target)) {
// if this is the closest set it as such
closest = currSum;
// skip duplicates
while (low < high && nums[low] == nums[low + 1]) {
low++;
}
while (low < high && nums[high] == nums[high - 1]) {
high--;
}
}
// increment runners
if (distance < 0) {
// increment low if the distance is negative
low++;
} else
// decrement high if the distance is positive
high--;
}
}
}
return closest;
}
}