[LeetCode]#1822. Sign of the Product of an Array

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.
Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
  1. init ans=1
  2. ans=ans x nums[i] by using loop
  3. if ans >0, return 1
  4. if ans <0, return -1
  5. if ans==0, return 0
class Solution:
def arraySign(self, nums):
ans=1
for i in range(len(nums)):
ans=ans*nums[i]
if ans >0:
return 1
if ans <0:
return -1
if ans ==0:
return 0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store