码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode-628-Maximum Product of Three Numbers]

时间:2017-06-25 12:00:35      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:bit   product   element   pre   sig   signed   output   length   nts   

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.

思路:

首先排序,然后分别判断数组元素最大值是正是负情况。

  int maximumProduct(vector<int>& nums)
  {

    sort(nums.begin(),nums.end());
    int len = nums.size();
    
    int a,b,c;
    c = nums[len-1];
    b = nums[len-2];
    a = nums[len-3];
    if(a>0)return  max(nums[0]*nums[1]*c,a*b*c);
    else if( a ==0 )
    {
      if(len==3)return 0;
      if(len>=5)return nums[len-5]*nums[len-4]*c;//l两个负数
      else return a*b*c;
    }
    else if(a<0)
    {
      if(c<0 )return a*b*c;
      if(c>=0 &&b<0 )return nums[0]*nums[1]*c;
      if(c>=0 && b>0 &&len>=4)return nums[0]*nums[1]*c;
      if(c>=0 && b>0 &&len==3)return a*b*c;
    }
    
    return 0;
  }
  

感觉写出来 超级啰嗦 惨不忍睹,于是看到了如下代码,

醍醐灌顶,五体投地。 排序过后,依次讨论前三个,后三个,以及后两个跟第一个,前两个跟最后一个。

 public int maximumProduct(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            int s = nums[n-1] * nums[n-2] * nums[n-3];
            s = Math.max(s, nums[n-1] * nums[n-2] * nums[0]);
            s = Math.max(s, nums[n-1] * nums[1] * nums[0]);
            s = Math.max(s, nums[2] * nums[1] * nums[0]);
            return s;
        }

 

[leetcode-628-Maximum Product of Three Numbers]

标签:bit   product   element   pre   sig   signed   output   length   nts   

原文地址:http://www.cnblogs.com/hellowooorld/p/7076369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!