给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 复制代码
示例:
输入: [1,2,3] 输出: 6 输入: [1,2,3,4] 输出: 24 复制代码
思考:
最大乘积的三个数可能有两种情况: 1.最大的三个正数相乘。 2.最小两个负数相乘再乘上最大的整数。 所以先将数组排序,然后按以上条件取两种情况之中较大的数。 复制代码
实现:
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]);
}
}复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Parsing Techniques
Dick Grune、Ceriel J.H. Jacobs / Springer / 2010-2-12 / USD 109.00
This second edition of Grune and Jacobs' brilliant work presents new developments and discoveries that have been made in the field. Parsing, also referred to as syntax analysis, has been and continues......一起来看看 《Parsing Techniques》 这本书的介绍吧!