作者: 负雪明烛
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:

  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.

题目大意

从一个数组中找出三个数字,求这三个数字的乘积是最大的值。

解题方法

方法一:排序

这个题要求数组中三个数乘积最大的值。我觉得可以从为什么问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)的更多相关文章

  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&Python] Problem 628. Maximum Product of Three Numbers

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

  7. 628. Maximum Product of Three Numbers

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

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

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

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

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

随机推荐

  1. RabbitMQ消息中介之Python使用

    本文介绍RabbitMQ在python下的基本使用 1. RabbitMQ安装,安装RabbitMQ需要预安装erlang语言,Windows直接下载双击安装即可 RabbitMQ下载地址:http: ...

  2. karatsuba乘法

    karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.[1] 此算法主要用于两个 ...

  3. docker可视化管理Portainer

    Portainer是一款轻量级docker容器管理平台,占用资源少,支持集群,支持权限分配. $ docker volume create portainer_data$ docker run -d ...

  4. A Child's History of England.41

    When intelligence of this new affront [hit in the face, c-o-n-frontation!] was carried to the King i ...

  5. Spark(二十一)【SparkSQL读取Kudu,写入Kafka】

    目录 SparkSQL读取Kudu,写出到Kafka 1. pom.xml 依赖 2.将KafkaProducer利用lazy val的方式进行包装, 创建KafkaSink 3.利用广播变量,将Ka ...

  6. 内存管理——array new,array delete

    1.array new array new就是申请一个数组空间,所以在delete的时候一定不能忘记在delete前加[] delete加上[]符号以后,就相当于告诉系统"我这里是数组对象, ...

  7. [转]C++中const的使用

    原文链接:http://www.cnblogs.com/xudong-bupt/p/3509567.html 平时在写C++代码的时候不怎么注重const的使用,长久以来就把const的用法忘记了 写 ...

  8. zabbix之源码安装

    #:官网地址 https://www.zabbix.com/documentation/4.0/zh/manual/installation/install #:解压并创建用户 root@ubuntu ...

  9. rust方法集

    随机数.数字对比.控制台输入 use std::io; use std::cmp::Ordering; use rand::Rng; fn main() { println!("please ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】chain

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/chain.py #!/usr/bin/env pytho ...