-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSumClosest16.java
More file actions
38 lines (34 loc) · 1.15 KB
/
ThreeSumClosest16.java
File metadata and controls
38 lines (34 loc) · 1.15 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
import java.util.Arrays;
public class ThreeSumClosest16 {
public static void main(String[] args) {
int[] a= {1,2,4,8,16,32,64,128};
threeSumClosest(a,82);
}
public static int threeSumClosest(int[] nums, int target) {
int len = nums.length;
int res =0;
if(target<Integer.MIN_VALUE || target>Integer.MAX_VALUE) throw new IllegalArgumentException("target不合适");
if(target>=0){
res=Integer.MAX_VALUE;
}else{
res=Integer.MIN_VALUE;
}
Arrays.sort(nums); //先排序
//暴力遍历法
for(int i=0;i<len-2;i++){
for(int j=i+1;j<len-1;j++){
Boolean flag =true; //添加一个终止标志,提前结束内循环
for(int k=j+1;k<len && flag;k++){
//for(int k=j+1;k<len ;k++){
flag = false;
int sum = nums[i]+nums[j]+nums[k];
if(Math.abs(sum-target)<Math.abs(res-target)){
res=sum;
flag =true;
}
}
}
}
return res;
}
}