[leetcode-628-Maximum Product of Three Numbers]
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:
- The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
- 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]的更多相关文章
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)
题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [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. ...
- 628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] 628. Maximum Product of Three Numbers_Easy
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- WPF 简易手风琴 (ListBox+Expander)
概述 之前听说很多大神的成长之路,几乎都有个习惯--写博文,可以有效的对项目进行总结.从而提高开发的经验.所以初学WPF的我想试试,顺便提高一下小学作文的能力.O(∩_∩)O哈哈~ 读万卷书不如行万里 ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- DNA比对算法:BWT
DNA比对算法:BWT BWT算法,实质上是前缀树的一种实现.那么什么是前缀树呢? 一.前缀树 对于问题p in S?如果S=rpq,那么p为S前缀rp的一个后缀. 于是,为了判断p in S 是否成 ...
- 如何搭建易企秀H5平台?
导读 易企秀如何开启伪静态支持? 一秀如何开启伪静态? 下载易企秀源码 oschina: http://git.oschina.net/jsper/html5Editor Windows下搭建环境 安 ...
- Cassandra Issue with Tombstone
1. Cassandra is quicker than postgre and have lower change to lose data. Cassandra doesn't have fore ...
- Python 随机生成有效手机号码及身份证
中国那么大,人那么多,几乎人手一部手机.手机号码已经作为各大互联网站的注册账户.同样,身份证更是如此.以下是生成有效手机号码和身份证号. 身份证需要下载districtcode.txt这个文件:htt ...
- android 本地数据库sqlite的封装
单机android sqlite数据库的实现,这个数据库可与程序一起生成在安装包中 一.下载sqlite3.exe文件 二.运行 cmd 转到sqlite3.exe 所在目录 运行 sqlite ...
- 图的M着色问题
问题描述: 给定无向连通图 G 和 m 种不同的颜色.用这些颜色为图 G 和各顶点着色,每个顶点着一种颜色.是否有一种着色法使得图 G 中每条边的两个顶点着不同的颜色.这个问题是图的 m 可着色判定问 ...
- JavaScript设计模式_01_单例模式
最近项目不太忙,难得有时间看看书,平时挺喜欢js这门语言.也看过很多高级教程,觉得自己还是比较热衷于js的设计模式.这一次重温一下<JavaScript设计模式与开发实践>,开篇为单例模式 ...
- 利用document的readyState去实现页面加载中的效果
打开新的网页时,为了增强友好性体验,告知用户网页正在加载数据需要呈现一个"页面加载中"之类的提示,只需要利用document就可以实现. 实现示例代码如下: <style&g ...