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

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.

原题地址: Maximum Product of Three Numbers

难度: Easy

题意: 找出相乘最大的三个数

思路:

因为数字有正有负,因此相乘最大的三个数分为两种情况:

(1)最大的三个正数

(2)最小的两个负数以及一个最大的正数

代码:

class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = b = c = None
d = e = 0x7FFFFFFF
for i in range(len(nums)):
if nums[i] >= a: # 找出最大的三个数
a, b, c = nums[i], a, b
elif nums[i] >= b:
b, c = nums[i], b
elif nums[i] >= c:
c = nums[i] if nums[i] <= e:      # 找出最小的两个数    
d, e = e, nums[i]
elif nums[i] <= d:
d = nums[i] max_val = 0 # if a > 0 and b > 0 and c > 0:
# max_val = max(max_val, a * b * c) # if a > 0 and d < 0 and e < 0:
# max_val = max(max_val, a * d * e) # if a < 0 and b < 0 and c < 0:
# max_val = a * b * c
max_val = max(a*b*c, a*d*e)
return max_val

时间复杂度: O(n)

空间复杂度: O(1)

628. Maximum Product of Three Numbers@python的更多相关文章

  1. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  2. [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. ...

  3. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  4. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

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

  5. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

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

  6. 628. Maximum Product of Three Numbers

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

  7. LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

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

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

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

  9. [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积

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

随机推荐

  1. Ruby测试小代码[计算50以内的素数]

    算法思想 判断某一个数,能不能被比他平方根小的素数整除. 首先看看代码 $arr = [] $arr[0] = 2 def add_prime(n) 3.step(n,2){|num| $arr &l ...

  2. 计算机网络自顶向下方法第2章-应用层(application-layer).1

    2.1 应用层协议原理 2.1.1网络应用程序体系结构 1)在客户-服务器体系结构 (client-server architecture)中,有一个总是打开的主机称为服务器,它服务于来自许多其他称为 ...

  3. boot接入elasticsearch

    boot接入elasticsearch 参考博客:https://blog.csdn.net/li521wang/article/details/83792552 项目源码demo:https://g ...

  4. 10M光纤与下载速度KB/s、MB/s的换算

    我们经常听说谁开的宽带是4M或10M或20M等等.那这和我们所说的网速“多少MB/s”.“多少KB/s”等同吗? 其实这不是一个概念. 在宽带运营商那里开的宽带,比如4M,其实是说4M bit/s,即 ...

  5. 未知宽高div水平垂直居中的3种方法

    方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  6. C# 常量与只读属性的区别

    public readonly string name ----这个name是个只读属性,不需要在定义时初始化值,而是可以在构造函数中完成初始化. public const int age =18   ...

  7. python对数据库的操作

    一 Python 操作 MySQL import pymysql pip install pymysql (1) 连接MySQL数据库   db = pymysql.connect(主机名,用户名,密 ...

  8. 从navicat for mysql导出数据库语句时应该加上的两条语句

    为了不引起编码问题,一般在从navict for mysql导出一个数据库时在文件最前面添加这2句语句: CREATE DATABASE IF NOT EXISTS `` default charac ...

  9. SQL server函数

    一般在开发中用到的函数 标量函数用的比较多 标量函数:就是返回一个单一的结果值 下面介绍一下标量函数的语法 create function GetFunction  --创建函数 (   @name ...

  10. Ubuntu系统下安装字体和切换默认字体的方法

    参考链接:http://my.oschina.net/itblog/blog/278566 打开Ubuntu的软件中心,搜索:tweak,安装[Unity Tweak Tool]这款软件,如图(由于我 ...