[Array]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.
思路:给出一个数组找出里面三个元素乘积的最大值并输出。先对整个数组进行排序,考虑到有负数的情况,因此,三个数的乘积有两种情况,2负1正,或者3个正数,如果只三个数便直接输出。
自己代码:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
if(nums[] < && nums[] < && nums[]*nums[]*nums[n-] > nums[n-]*nums[n-]*nums[n-])
return nums[]*nums[]*nums[n-];
else
return nums[n-] * nums[n-] * nums[n-];
}
优秀代码:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int m1 = nums[]*nums[]*nums[n-];
int m2 = nums[n-] * nums[n-] * nums[n-];
return m1 > m2?m1:m2; //或者 return max(m1, m2);
}
其实不用检验前面两个元素是否是负数,因为只有两种情况,前面两个,后面一个,或者后面三个。
[Array]628. Maximum Product of Three Numbers的更多相关文章
- 【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 (最大三数乘积)
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&Python] Problem 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] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- Activiti表单(Form key)
1.设置Form key如图: 2.根据任务id得到Form key TaskFormData formData = formService.getTaskFormData(taskId);; Str ...
- Linq学习笔记(第一部分)
本文分享自lliulun的30分钟linq教程,地址:http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html 一:与LINQ有关的语 ...
- LOJ#6075. 「2017 山东一轮集训 Day6」重建
题目描述: 给定一个 n个点m 条边的带权无向连通图 ,以及一个大小为k 的关键点集合S .有个人要从点s走到点t,现在可以对所有边加上一个非负整数a,问最大的a,使得加上a后,满足:s到t的最短路长 ...
- mysql 主从,双主同步
1.创建用户并设置远程访问授权 1). A上添加: //ip地址为B的ip地址,用于B访问 ' with grant option; 2). B上添加://ip地址为A的ip地址,用于A访问 ' wi ...
- memcache课程---1、memcache介绍及安装(memcache作用)
memcache课程---1.memcache介绍及安装(memcache作用) 一.总结 一句话总结: 减少对数据库的访问,因为数据库的访问比较花费时间 1.memcache为什么比操作数据库快的多 ...
- Maven的作用及简介
Maven的作用及简介 一.maven作用 项目之间都是有依赖的,比如A项目依赖于B项目,B项目依赖与C.D项目,等等.这样的依赖链可能很长. 但是,没有一个项目的jar包我们都要导入进去,我们要做的 ...
- CDATA标签用法
今天在xml文件里看到有CDATA标签的使用, 答案如下: CDATA 术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data). 在 X ...
- Docker在线文档收集
极客学院 kubernetes中文社区 易百教程
- LocalSessionFactoryBean有几个属性查找hibernate映射文件
LocalSessionFactoryBean有几个属性查找hibernate映射文件: mappingResources.mappingLocations.mappingDirectoryLocat ...
- Hack Tools
Tools 2011-03-17 13:54:36| 分类: Security|举报|字号 订阅 Packet Shaper:Nemesis: a command line packet s ...