-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.最接近的三数之和.java
42 lines (38 loc) · 1.01 KB
/
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
37
38
39
40
41
import java.util.Arrays;
/*
* @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 n = nums.length;
int res = nums[0]+nums[1]+nums[n-1];
for(int i=0;i<nums.length-1;i++){
int p = i+1,q = n-1;
int resi = nums[p]+nums[q]+nums[i];
boolean flag = resi>target;
while(p<q){
int tmp = nums[p]+nums[q]+nums[i];
if(tmp ==target){
return tmp;
}
if(flag!=(tmp>target)){
flag=tmp>target;
}
if(Math.abs(target-tmp)<Math.abs(target-res)){
res = tmp;
}
if(flag){
q--;
}else{
p++;
}
}
}
return res;
}
}
// @lc code=end