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-];
b = nums[len-];
a = nums[len-];
if(a>)return max(nums[]*nums[]*c,a*b*c);
else if( a == )
{
if(len==)return ;
if(len>=)return nums[len-]*nums[len-]*c;//l两个负数
else return a*b*c;
}
else if(a<)
{
if(c< )return a*b*c;
if(c>= &&b< )return nums[]*nums[]*c;
if(c>= && b> &&len>=)return nums[]*nums[]*c;
if(c>= && b> &&len==)return a*b*c;
} return ;
}

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

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

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

[leetcode-628-Maximum Product of Three Numbers]的更多相关文章

  1. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

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

  2. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

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

  3. LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

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

  4. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  5. 628. Maximum Product of Three Numbers@python

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

  6. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  7. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

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

  8. 628. Maximum Product of Three Numbers

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

  9. [LeetCode] 628. Maximum Product of Three Numbers_Easy

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

  10. [Array]628. Maximum Product of Three Numbers

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

随机推荐

  1. PS不能存储,因为程序错误

    当PS中遇到不能存储文件,因为程序错误时,可以这样: http://www.zcool.com.cn/article/ZMTgwOTQw.html

  2. XSHELL工具上传文件到Linux以及下载文件到本地(Windows)

    Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的Linux上需要安装安装lrzsz工具包,(如果没有安装请执行以下命令,安装 ...

  3. 机器学习:从编程的角度理解BP神经网络

    1.简介(只是简单介绍下理论内容帮助理解下面的代码,如果自己写代码实现此理论不够) 1) BP神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...

  4. xml注释快捷键

    eclipse中编辑Java或C/C++文件时,注释的快捷键均为 "CTRL + / ",编辑xml文件时,该快捷键无效. eclipse XML 注释:CTRL + SHIFT ...

  5. unity 判断是安卓还是IOS平台

    功能概述 储值功能,点击一个按钮(mSprites["Recharge"]),实现储值功能,在IOS平台上,该按钮下的功能全部隐藏,在安卓平台上正常显示按钮,实现相应功能 #if ...

  6. asp.net core利用DI实现自定义用户系统,脱离ControllerBase.User

    前言 很多时候其实我们并不需要asp.net core自带的那么复杂的用户系统,基于角色,各种概念,还得用EF Core,而且在web应用中都是把信息存储到cookie中进行通讯(我不喜欢放cooki ...

  7. 一个gif远程crash你的微信!

    测试了一下iPhone6,iPhone7不同版本的 iOS上,收到某个天线宝宝的gif,最新版本的微信都会挂. demo视频:演示视频 通过 crash log可以看到微信最新的6.5.8版本在打开 ...

  8. 关于微信小程序的的总结

    微信小程序学完了,给大家分享一些自己学小程序的心得,希望能帮到大家. 首先,我谈谈小程序数据绑定的那一块,所有从本地或者远程服务器的API传过来,都必须绑定到data: {}, 绑定格式是一个一个的键 ...

  9. springboot redis 缓存对象

    只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...

  10. Spring学习笔记——01 控制反转

    想一下之前学的Java,如果某个类需要引用某个对象,则需要手动new一个出来.这样带来的一个问题就是,若被引用的类发生改动或被删除,则引用它的所有类都会报错.因为两个类耦合在一起了.解决的办法就是不由 ...