package suanfa.Max;
public class MaxProduct {
public static void main(String[] args) {
System.out.println(getMaxMin(new int[]{1,3,-1,-3,4,2}));
}
private static int getMaxMin(int[] nums) {
// TODO Auto-generated method stub
int min1=Integer.MAX_VALUE,min2=Integer.MAX_VALUE; //定义两个最小的
int max1=Integer.MIN_VALUE,max2=Integer.MIN_VALUE,max3=Integer.MIN_VALUE;//定义三个最大的
for(int x:nums){
if(x<min1){
min2=min1;
min1=x;
}
if(x<min2){
min2=x;
}
}
//找最大的
for(int y:nums){
if(y>max3){
max1=max2;
max2=max3;
max3=y;
}
else if(y>max2){
max1=max2;
max2=y;
}
else if(y>max1){
max1=y;
}
}
return Math.max(min1*min2*max1, max1*max2*max3);
}
}
Java-数组中三个数最大值-线性扫描
最新推荐文章于 2022-07-22 11:42:14 发布