LeetCode : 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!做质因数分解n!=2x*3y*5z*…

显然0的个数等于min(x,z),并且min(x,z)==z

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

posted on 2017-03-31 13:58  gechen  阅读(80)  评论(0编辑  收藏  举报

导航