Maximum Product of Three Numbers
Input:
[1,2,3]
Output:
6Input:
[1,2,3,4]
Output:
24题目大意:
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
max_num = 0
# if all negative, or all positive, or only one negative number. In this case, take 3 rightmost number
if nums[-1] < 0 or nums[0] >= 0 or (nums[0] < 0 and nums[1] > 0):
max_num = nums[-1] * nums[-2] * nums[-3]
# if two or more negative numbers, compare rightmost 3 nums or 2 left most * 1 rightmost
else:
max_num = max(nums[-3] * nums[-2] * nums[-1], nums[0] * nums[1] * nums[-1])
return max_numLast updated