Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
一刷
題解:
求n!尾部有多少個0。其實主要就是看有多少個5,有多少個5就有多少個0
count = n / 5 + n / 25 + n / 125 + ....
如果有25,會產(chǎn)生2個0, 125會產(chǎn)生3個0
public class Solution {
public int trailingZeroes(int n) {
if(n<=0) return 0;
int res = 0;
while(n>0){
res += n/5;
n/=5;
}
return res;
}
}