题目表述:
题解1:
for循环比较:
def maximumProduct(self, nums):
lennums = len(nums)
maxmul = float("-inf")
for i in range(lennums):
for j in range(i+1,lennums):
for k in range(j+1,lennums):
res = nums[i]*nums[j]*nums[k]
if res>maxmul:
maxmul = res
return maxmul
所给样例通过,提交超时:
题解2:参考https://blog.csdn.net/Manson_Wang/article/details/82108860
对输入数组进行排序
输入数组有以下几种情况:
<1>均为正数,则最大的三个数的乘积最大
<2>有正有负,比较三个最大数的乘积与最大数和两个最小数之积的最大值
def maximumProduct(self, nums):
nums.sort()
max1 = nums[-1]
max2 = nums[-2]
max3 = nums[-3]
min1 = nums[0]
min2 = nums[1]
min3 = nums[2]
return max(max3*max2*max1,max1*min1*min2)