【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/maximum-product-of-three-numbers/description/
题目描述
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.
题目大意
从一个数组中找出三个数字,求这三个数字的乘积是最大的值。
解题方法
方法一:排序
这个题要求数组中三个数乘积最大的值。我觉得可以从为什么问3个数字而不是其他数字去考虑。
输入有可能存在负值,所以3个数字的乘积时会考虑到负负得正的情况。只有三个数都是正数或者有只有两个负数时得到的结果是正的。这样,首先通过排序,得到最右边三个数的乘积,和最小的两个负数(如果存在负数)和最大数字的乘积,比较两个乘积的大小就行了。
如果排序后取到的三个数存在奇数个负数也没关系,我们取最大值的时候会保证取到最大的。
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
right = nums[-3] * nums[-2] * nums[-1]
left = nums[0] * nums[1] * nums[-1]
return max(left, right)
日期
2018 年 1 月 26 日
2018 年 11 月 17 日 —— 美妙的周末,美丽的天气
【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)的更多相关文章
- [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&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. ...
随机推荐
- python 新闻管理系统——启示
mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...
- docker可视化管理Portainer
Portainer是一款轻量级docker容器管理平台,占用资源少,支持集群,支持权限分配. $ docker volume create portainer_data$ docker run -d ...
- C/C++ Qt StandardItemModel 数据模型应用
QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...
- Learning Spark中文版--第四章--使用键值对(2)
Actions Available on Pair RDDs (键值对RDD可用的action) 和transformation(转换)一样,键值对RDD也可以使用基础RDD上的action(开工 ...
- Scala(三)【函数式编程】
目录 一.方法和函数 1.方法 1)基本语法 2)简化原则 3)方法参数 2.函数 3.方法和函数的区别 二.高阶函数 三.匿名函数 四.柯里化 五.闭包 一.方法和函数 1.方法 1)基本语法 de ...
- canal从mysql拉取数据,并以protobuf的格式往kafka中写数据
大致思路: canal去mysql拉取数据,放在canal所在的节点上,并且自身对外提供一个tcp服务,我们只要写一个连接该服务的客户端,去拉取数据并且指定往kafka写数据的格式就能达到以proto ...
- HDFS初探之旅(二)
6.HDFS API详解 Hadoop中关于文件操作类疾病上全部在"org.apache.hadoop.fs"包中,这些API能够支持的操作包含:打开文件.读写文件.删除文件等. ...
- Advanced C++ | Virtual Copy Constructor
这个不懂,等看会了再写...
- Java操作csv文件
以前就一直很想搞懂一个问题就是java如何读取和写入csv文件,现在要花时间总结一波. 主要使用的javaCSV.jar javaCSV API:http://javacsv.sourceforge. ...
- hadoop accesscontrolException
DFS loaction: /tmp 文件下的 hadoop-haoop/mapred/system报 AccessControlException. 解决: bin/hadoop fs -chmod ...