GuilinDev

Lc1822

05 August 2008

1822 Sign of the Product of an Array

array中元素相乘后的乘积,正负零返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
    public int arraySign(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int count = 0;
        for (int num : nums) {
            if (num == 0) {
                return 0;
            } 
            if (num < 0) {
                count++;
            }
        }
        return count % 2 == 0 ? 1 : -1;
    }
}