This article is participating in Python Theme Month. See the link to the event for more details

describe

There is a function signFunc(x) that returns:

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

Example 1:

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
Copy the code

Example 2:

Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0
Copy the code

Example 3:

Input: nums = [-1,1,-1,1,-1] Output: -1 Explanation: The product of all values in The array is-1, and signFunc(-1) = -1Copy the code

Note:

1 <= nums.length <= 1000
-100 <= nums[i] <= 100
Copy the code

parsing

If the elements in NUMs are multiplied, they are greater than 0, less than 0, or equal to 0.

answer

class Solution(object):
    def arraySign(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        product = reduce(lambda x,y:x*y,nums)
        if product>0:
            return 1
        elif product<0:
            return -1
        return 0
        
        	      
		
Copy the code

The results

Runtime: 40 ms, the linked list for Sign of the Product of an Array. Submissions for Sign of the Product of an ArrayCopy the code

parsing

So if we take an arbitrary number n and multiply it by a negative number -m, we get -n times m, So the absolute value is going to go up, but it’s going to go negative, and in that case, all you have to do is judge the sign of the final result. Initialize the result to 1 and iterate through each element in nums if it is less than zero. If the element is 0, then 0 is returned. If the element is positive, then 0 is left unchanged. At the end of the traversal result is the final result.

answer

class Solution(object):
    def arraySign(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result = 1
        for num in nums:
            if num == 0:
                return 0
            if num < 0:
                result = -result
        return result
        	      
		
Copy the code

The results

Runtime: 40 ms, the linked list for Sign of the Product of an Array. Submissions for Sign of the Product of an ArrayCopy the code

parsing

In addition, we can use the built-in function reduce to directly carry out fast multiplication, and then judge the result is positive or negative or 0. This kind of thinking is the same as the first kind, but it is more convenient to use the built-in function.

answer

class Solution(object):
    def arraySign(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        product = reduce(lambda x,y: x*y, nums)
        return 0 if not product else 1 if product > 0 else -1

		
Copy the code

The results

Runtime: 10000 ms, the linked list for Sign of the Product of an Array. Submissions for Sign of the Product of an ArrayCopy the code

Link: leetcode.com/problems/si…

Your support is my greatest motivation