Sign of the Product of an Array
给一个数组, 求里面的数字乘积是正负还是0.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Solution { public int arraySign(int[] nums) { int count = 0; for(int n : nums){ if(n < 0) count++; if(n == 0) return 0; } return count % 2 == 0 ? 1 : -1; } } |