美文网首页
172. Factorial Trailing Zeroes

172. Factorial Trailing Zeroes

作者: 刘小小gogo | 来源:发表于2018-08-24 00:15 被阅读0次
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

      本文链接:https://www.haomeiwen.com/subject/repviftx.html