方法一:笨蛋方法,直接相乘
def arraySign(nums):
sum=1
for i in range(len(nums)):
sum=nums[i]*sum
if sum>0:
return 1
if sum<0:
return -1
if sum==0:
return 0
方法二:判断是否有0存在,
def arraySign(nums):sign=1for i in range(len(nums)):if nums[i]==0:sign=0if nums[i]<0:sign=-signreturn sign print(arraySign([2,3,4,-1,-9,-8,0]))