628. Maximum Product of Three Numbers@python
2021-06-16 00:06
标签:numbers lis a* 复杂 turn 两种 难度 一个 nts Given an integer array, find three numbers whose product is maximum and output the maximum product. Note: 原题地址: Maximum Product of Three Numbers 难度: Easy 题意: 找出相乘最大的三个数 思路: 因为数字有正有负,因此相乘最大的三个数分为两种情况: (1)最大的三个正数 (2)最小的两个负数以及一个最大的正数 代码: 时间复杂度: O(n) 空间复杂度: O(1) 628. Maximum Product of Three Numbers@python 标签:numbers lis a* 复杂 turn 两种 难度 一个 nts 原文地址:https://www.cnblogs.com/chimpan/p/9728933.html
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
# max_val = max(max_val, a * d * e)
# if a
# max_val = a * b * c
max_val = max(a*b*c, a*d*e)
return max_val
下一篇:9.30T1 排序不等式+逆元
文章标题:628. Maximum Product of Three Numbers@python
文章链接:http://soscw.com/index.php/essay/94350.html