class Solution(object):
def maxProduct(self, arr):
if len(arr) == 0:
return 0
if len(arr) == 1:
return arr[0]
# 以0为单位拆分
def deal(list_ori,p):
list_new=[]
list_short=[]
for i in list_ori:
if i!=p:
list_short.append(i)
else:
list_new.append(list_short)
list_short=[]
list_new.append(list_short)
return list_new
# 判断列表是否全负
def negative(arr):
for i in arr:
if i > 0:
return False
return True
BigNums = deal(arr, 0)
BigArr = []
for nums in BigNums:
if nums == []:
continue
base = 1
maxValue = nums[0]
for i in range(len(nums)):
base *= nums[i]
if maxValue < base:
maxValue = base
if nums[i] == 0:
base = 1
base = 1
for i in range(len(nums)-1, -1, -1):
base *= nums[i]
if maxValue < base:
maxValue = base
if nums[i] == 0:
base = 1
BigArr.append(maxValue)
# 最大值列表中 BigArr 如果全为负 且源数据存在0则 返回0
# 否则返回BigArr中最大值
return 0 if len(BigNums) >= 2 and negative(BigArr) else max(BigArr)
leetcode刷题250天(25)——152. 乘积最大子数组
最新推荐文章于 2024-08-31 00:26:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)