lc1822数组元素积的符号
将每个数累乘,可能会溢出。
其实。无论是乘以-100还是-1,对ans的贡献都是加一个负号,所以我们看负数有几个就OK
class Solution { public int arraySign(int[] nums) { int ans = 1; for(int i=0;i<nums.length;i++){ if(nums[i]==0){ return 0; } else if(nums[i]<0){ ans = -ans; } } return ans; } }
lc面试题16.05阶乘后0的个数
找0的个数 == 找2*5的对数 == 找5因子的个数(2更多,所以找5)
25! == 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
25/5 = 5
别忘了还有25,它可以拆成5*5
5/5 = 1
cnt = 6
class Solution { public int trailingZeroes(int n) { int cnt = 0; while(n>0){ n /= 5; cnt+=n; } return cnt; } }