leetcode 628. Maximum Product of Three Numbers

来源:互联网 发布:淘宝分销商招募书 编辑:程序博客网 时间:2024/05/18 02:13

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]Output: 6

Example 2:

Input: [1,2,3,4]Output: 24

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

要找到使乘积最大的三个数,我是想到,要分大于0和小于0的数。其中如果不存在大于0的数,那就取小于0的数中最大的三个相乘,乘积最大;如果存在大于0的数,那么大于0的最大的那个数,肯定要被算入最终结果的因子中的。接着看“小于0中绝对值最大的两个数相乘”是否比“大于0中第二大和第三大的两个数相乘”大,哪个大选哪个。

package leetcode;public class Maximum_Product_of_Three_Numbers_628 {public int maximumProduct(int[] nums) {if(nums.length==3){return nums[0]*nums[1]*nums[2];}int[] three_Positive=new int[3];int[] three_negaive=new int[3];int[] three_negative_max=new int[]{Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};for(int i=0;i<nums.length;i++){int the=nums[i];if(the>=0){if(the>three_Positive[0]){three_Positive[0]=the;if(the>three_Positive[2]){swap(three_Positive, 0, 1);swap(three_Positive, 1, 2);}else if(the>three_Positive[1]){swap(three_Positive, 0, 1);}}}else{if(the<three_negaive[0]){three_negaive[0]=the;if(the<three_negaive[2]){swap(three_negaive, 0, 1);swap(three_negaive, 1, 2);}else if(the<three_negaive[1]){swap(three_negaive, 0, 1);}}if(the>three_negative_max[0]){three_negative_max[0]=the;if(the>three_negative_max[2]){swap(three_negative_max, 0, 1);swap(three_negative_max, 1, 2);}else if(the>three_negative_max[1]){swap(three_negative_max, 0, 1);}}}}int product=1;    if(three_Positive[2]>0){    product=three_Positive[2];    if(three_Positive[0]*three_Positive[1]>three_negaive[1]*three_negaive[2]){product=product*three_Positive[0]*three_Positive[1];}else{product=product*three_negaive[1]*three_negaive[2];}    }    else{    product=three_negative_max[0]*three_negative_max[1]*three_negative_max[2];    }return product;}public void swap(int[] three,int i,int j){int temp=three[i];three[i]=three[j];three[j]=temp;}public static void main(String[] args) {// TODO Auto-generated method stubMaximum_Product_of_Three_Numbers_628 m=new Maximum_Product_of_Three_Numbers_628();int[] nums=new int[]{-4,-3,-2,-1,60};int[] nums1=new int[]{1,0,100};int[] nums2=new int[]{0,0,0,4};int[] nums3=new int[]{-5,-4,-3,-2,-1};System.out.println(m.maximumProduct(nums3));}}
大神的方法更简洁:找出数组中最大的三个数和最小的两个数。完美地归纳了我之前考虑的种种情况:其中如果不存在大于0的数,那结果就是max1*max2*max3;如果存在大于0的数,那么大于0的最大的那个数即max1,肯定要被算入最终结果的因子中的。接着看“小于0中绝对值最大的两个数相乘,即min1*min2”是否比“大于0中第二大和第三大的两个数相乘即max2*max3”大,哪个大选哪个。
public int maximumProduct1(int[] nums) {   int length=nums.length;   if(length<3){   return 0;   }   else if(length==3){   return nums[0]*nums[1]*nums[2];   }        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MIN_VALUE;        for (int n : nums) {            if (n > max1) {                max3 = max2;                max2 = max1;                max1 = n;            } else if (n > max2) {                max3 = max2;                max2 = n;            } else if (n > max3) {                max3 = n;            }            if (n < min1) {                min2 = min1;                min1 = n;            } else if (n < min2) {                min2 = n;            }        }        return Math.max(max1*max2*max3, max1*min1*min2);    }


阅读全文
0 0
原创粉丝点击