. - 力扣(LeetCode)

直接把所有非0的数字相乘,负数若为基数则除以最大的负数即可,然后需要考虑数组长度为1时,和数组中无正数的情况等

from typing import List
 
 
class Solution:
	def maxStrength(self, nums: List[int]) -> int:
		n = len(nums)
		if n == 1:
			return nums[0]
		max_negative = -10
		max_value = 1
		negative_count = 0
		max_positive = -1
		for score in nums:
			if 0 > score:
				negative_count = negative_count + 1
				if score > max_negative:
					max_negative = score
			elif max_positive < score:
				max_positive = score
			if score != 0:
				max_value = max_value * score
		if max_positive == 0 and negative_count <= 1:
			return 0
		if negative_count % 2 == 0:
			return max_value
		else:
			return max_value // max_negative