https://leetcode.com/problems/factorial-trailing-zeroes/description/
Solution. The number of trailing 0 of n! equals the total number of factor 5 in 1, 2, ... , n.
Each step, n/5 is the number of integers that have at least one factor of 5 in 1,2,...,n.
int trailingZeroes(int n) {
if(n == 0) return 0;
int nz = 0;
while(n) {
nz += n/5;
n /= 5;
}
return nz;
}
No comments:
Post a Comment