-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.最接近的三数之和.java
37 lines (35 loc) · 953 Bytes
/
16.最接近的三数之和.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
/*
* @lc app=leetcode.cn id=16 lang=java
*
* [16] 最接近的三数之和
*/
// @lc code=start
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = 0;
boolean first = true;
for(int i = 0; i <= nums.length-3;i++){
int low = i+1;
int high = nums.length-1;
while(low<high){
int temp = nums[i] + nums[low] + nums[high];
if(first){
res = temp;
first = !first;
}else{
res = Math.abs(target-res)>Math.abs(target-temp)?temp:res;
}
if(temp-target < 0){
low++;
}else if(temp - target > 0){
high--;
}else{
return target;
}
}
}
return res;
}
}
// @lc code=end