-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution16.java
executable file
·52 lines (43 loc) · 1.28 KB
/
Solution16.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
41
42
43
44
45
46
47
48
49
50
51
52
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by slade on 2019/6/5.
*/
public class Solution16 {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int length = nums.length;
int res = nums[0] + nums[1] + nums[length - 1];
for (int i = 0; i < length - 2; i++) {
int first = i + 1;
int last = length - 1;
if (i != 0 && nums[i - 1] == nums[i]) {
continue;
}
while (first < last) {
int cur = nums[first] + nums[last] + nums[i];
if (target == cur) {
return target;
}
if (Math.abs(target - cur) < Math.abs(target - res)) {
res = cur;
}
if ((target - cur) > 0) {
first++;
}
if ((target - cur) < 0) {
last--;
}
}
}
return res;
}
public static void main(String[] args) {
int[] a = {0, 2, 1, -3};
Solution16 s = new Solution16();
System.out.println(s.threeSumClosest(a, 1));
}
}