题解 | #三个数的最大乘积#
三个数的最大乘积
http://www.nowcoder.com/practice/8ae05c2913fe438b8b14f3968f64fc0b
import java.util.*;
public class Solution { /** * 最大乘积 * @param A int整型一维数组 * @return long长整型 / public long solve (int[] A) { // write code here int max1= Integer.MIN_VALUE,max2= Integer.MIN_VALUE,max3= Integer.MIN_VALUE; int min1 = Integer.MAX_VALUE,min2 = Integer.MAX_VALUE; for(int num:A){ if(num > max1){ max3 = max2; max2 = max1; max1 = num; }else if(num > max2){ max3 = max2; max2 = num; }else if(num > max3){ max3 = num; } if(num < min1){ min2 = min1; min1 = num; }else if(num < min2){ min2 = num; } } return Math.max((long)max1max2max3,(long)max1min1*min2); } }
我居南半坡 文章被收录于专栏
多刷题,积蓄力量,欢迎讨论