172. Factorial Trailing Zeroes

2018-08-24  本文已影响0人  刘小小gogo
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;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读