[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. ...
随机推荐
- ——————————JavaScript中,对String字符串的一些操作——————————
————————————————————————————————————————————————————————————————————————————————————————————— <ht ...
- [ios]quartz2d画板功功能实现核心代码
//触摸开始 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 1,获取对 ...
- jqPaginator 项目中做分页的应用技巧
最近做后台管理系统,分页用到的不少,项目中其实已经有分页功能的组件但是不够高度自定义,于是就找到了 jqPaginator 高度自定义的Html结构 初始化引用如下: $("#paginat ...
- ER模型的学习
(计应154兰家才)在建立表后开始了学习的第一步,了解数据库的基本知识,就以建立bbs论坛为标准学习er模型,首先建立了4个表分别是BBSReply,BBSSection,BBSTopic,BBSUs ...
- Android 模块化探索与实践
首发于<程序员>杂志五月刊 一.前言 万维网发明人 Tim Berners-Lee 谈到设计原理时说过:"简单性和模块化是软件工程的基石:分布式和容错性是互联网的生命." ...
- 初识Java反射
要详细的了解Java反射,就得要了解Java的类加载以及何为运行时动态加载等等概念.本文抛开其余概念,简单介绍Java反射,详细介绍会在以后有一个系统而全面的认识过后展开. 反射是Java被视为动态语 ...
- 【JAVAWEB学习笔记】27_Redis:在Linux上的安装、Jedis和常用命令
一.Redis简介 1.关于关系型数据库和nosql数据库 关系型数据库是基于关系表的数据库,最终会将数据持久化到磁盘上,而nosql数据 库是基于特殊的结构,并将数据存储到内存的数据库.从性 ...
- Linux services, runlevels, and rc.d scripts
Reference: [1] https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts A Linux se ...
- 获取openId | 小程序
最近项目中需要使用微信授权,继上一篇<关于微信小程序拒绝授权后,重新授权并获取用户信息>之后,需要获取用户的openId,开发测试时,发现无论如何都获取不到: 官方文档如下: 相信很多同学 ...
- 浅谈C语言指针
下面就几种情况讨论指针. 一.指针和变量 变量是存储空间的别名,访问形式是直接访问. 指针访问内存地址是间接访问. 使用指针访问内存的场合:1.局部变量,参数传递 2.动态分配内存 指针本身也是 ...