172. Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

求n得阶层,末尾0的个数

老问题了,统计2,5的个数,明显2的个数要多。

只要统计5的个数就可以了,5的个数 = sum(n/5^1,  n/5^2, n/5^3...) 也就是 1~n中包含多少个5,25,125,5^x 

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

 

posted on 2017-07-25 15:21  Beserious  阅读(70)  评论(0编辑  收藏  举报