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. [Xcode 实际操作]一、博主领进门-(7)使用不同类型的iOS模拟器

    目录:[Swift]Xcode实际操作 本文将演示使用不同类型的iOS模拟器. 点击[运行]按钮,打开模拟器,并预览当前的项目. 当向苹果商店提交应用时,也需要同时提交应用的截图. 对当前的应用的界面 ...

  2. AVAudioPlayer 如何在页面呈现之后按需初始化

    在页面中按需初始化 AVAudioPlayer 很多时候我们需要根据页面上内容的情况创建 AVAudioPlayer 对象,已达到降低无谓资源占用等目的.下面我们来看一段代码看起来正确的代码: ove ...

  3. 2.Python基础认识(格式化输出,while语句,运算符,编码,单位转化)

    Python基础认识 1.字符串的格式化初识及占位符的简单应用 字符串的格式化 按照既定的要求进行有规定排版的一种输出方式. #我们想要输出的格式如下: ----------------------- ...

  4. C 语言实例 - 计算两个时间段的差值

    C 语言实例 - 计算两个时间段的差值 C 语言实例 C 语言实例 计算两个时间段的差值. 实例 #include <stdio.h> struct TIME { int seconds; ...

  5. android BottomNavigationView 底部显示3个以上的item

    你现在可以用app:labelVisibilityMode="[labeled, unlabeled, selected, auto] labeled 所有的标签都是可见的. unlabel ...

  6. linux常用命令(ubuntu)

    编辑: vi [path] vim [path] :q 退出 :wq 保存退出 查看进程 ps ps -aux | grep mem 查看全部含 “mem”的进程 ps –aux  查看全部 在系统启 ...

  7. Maximum Control (medium) Codeforces - 958B2

    https://codeforces.com/contest/958/problem/B2 题解:https://www.cnblogs.com/Cool-Angel/p/8862649.html u ...

  8. python之序列化json模块与pickle模块(待补充)

    一.json是所有语言都通用的一种序列化格式 只支持 : 列表,字典字符串,数字,且字典的key必须是字符串 ''' 1. dumps , loads 在内存中做数据转换: dumps : 数据类型 ...

  9. C8051F单片机定时器的定时

    假设C8051F020单片机的晶振是sysclk=22114800HZ,即每秒计22114800个数经过Div=12分频后得到定时器的计数频率Tclk=sysclk/12,每秒计22114800÷12 ...

  10. jasmine+karma 自动化单元测试

    测试的必须性 相信大家都知道测试的必要性,测试先行的概念.不过,写了这么多年的代码,除了之前用java的时候写过一些测试用例,还真的很少写过前端的测试用例,或者做一些自动化测试.感觉做单元测试还是很有 ...