-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution016.java
50 lines (42 loc) · 1.26 KB
/
Solution016.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
package algorithm.leetcode;
import java.util.Arrays;
/**
* @author: mayuan
* @desc: 最接近的三数之和
* @date: 2018/07/11
*/
public class Solution016 {
public static void main(String[] args) throws Exception {
int[] nums = new int[]{-3, -2, -5, 3, -4};
int target = -1;
System.out.println(new Solution016().threeSumClosest(nums, target));
}
public int threeSumClosest(int[] nums, int target) {
if (null == nums || 3 > nums.length) {
return 0;
}
Integer ans = null;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum > target) {
--right;
} else if (sum < target) {
++left;
} else {
ans = sum;
return ans;
}
if (null == ans) {
ans = sum;
} else if (Math.abs(sum - target) < Math.abs(ans - target)) {
ans = sum;
}
}
}
return ans;
}
}