Skip to main content

1822. Sign of the Product of an Array

https://leetcode.com/problems/sign-of-the-product-of-an-array/

Python

class Solution:
def arraySign(self, nums: List[int]) -> int:
result = 1
for num in nums:
if num == 0:
return 0
if num < 0:
result = -result
return result

Go

func arraySign(nums []int) int {
result := 1
for _, num := range nums {
if (num == 0) {
return 0
}
if (num < 0) {
result = -result
}
}
return result
}

Rust

impl Solution {
pub fn array_sign(nums: Vec<i32>) -> i32 {
let mut result = 1;
for num in &nums {
if *num == 0 {
return 0;
}
if *num < 0 {
result = -result;
}
}
result
}