172. Factorial Trailing Zeroes

172. Factorial Trailing Zeroes_第1张图片
image.png

Trailing 0s in n! = Count of 5s in prime factors of n!
= floor(n/5) + floor(n/25) + floor(n/125) + ....

class Solution {
public:
    int trailingZeroes(int n) {
        if(n == 0) return 0;
        int count = 0;
        while(n){
            count += n/5;
            n /= 5;
        }
        return count;
    }
};

你可能感兴趣的:(172. Factorial Trailing Zeroes)