forked from yuanguangxin/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
46 lines (40 loc) · 1.21 KB
/
Solution.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
package 双指针遍历.q16_最接近的三数之和;
import java.util.Arrays;
/**
* q15类型题 数组遍历 + 双指针遍历 o(n^2)
*/
public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int rs = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
int c = sum - target;
if (Math.abs(c) < Math.abs(rs - target)) {
rs = sum;
}
if (c == 0) {
return target;
} else if (c > 0) {
right--;
} else {
left++;
}
}
}
return rs;
}
public static void main(String[] args) {
int[] a = new int[]{-3, -2, -5, 3, -4};
System.out.println(new Solution().threeSumClosest(a, -1));
}
}