-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (25 loc) · 734 Bytes
/
Solution.java
File metadata and controls
29 lines (25 loc) · 734 Bytes
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
package leetCode_16;
import java.util.Arrays;
/**
* @author dimdark
* @date 2017-08-27
* @time 9:49 AM
*/
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int i, j, k, temp;
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for(i = 0; i < nums.length-2; ++i) { // reduce unpossible cases
j = i+1; k = nums.length-1;
while(j < k) {
temp = nums[i] + nums[j] + nums[k];
ans = Math.abs(target-ans)>Math.abs(target-temp) ? temp : ans;
if(temp < target) j++;
else if(temp > target) k--;
else return ans;
}
}
return ans;
}
}