-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathThreeSumClosest.java
More file actions
39 lines (34 loc) · 1.02 KB
/
ThreeSumClosest.java
File metadata and controls
39 lines (34 loc) · 1.02 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
package twopointers;
// Source : https://leetcode.com/problems/3sum-closest/
// Id : 16
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-01-01
// Topic : Two Pointers
// Level : Medium
// Other :
// Tips :
// Result : 85.38% 17.73%
import java.util.Arrays;
public class ThreeSumClosest {
// 5ms
public int threeSumClosest1(int[] nums, int target) {
// public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length; i++) {
int l = i + 1, r = nums.length - 1, sum = 0;
while (l < r) {
sum = nums[l] + nums[r] + nums[i];
if (Math.abs(target - sum) < Math.abs(target - res))
res = sum;
if (sum > target)
r--;
else if (sum < target)
l++;
else
return res;
}
}
return res;
}
}