172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

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

Note: Your solution should be in logarithmic time complexity.

(1)

class Solution {
public:
    int trailingZeroes(int n) {
        int ans = 0;
        for(long long i = 5; n / i; i *= 5)
        {
            ans += n / i;
        }
        return ans;
    }
};

(2)

class Solution {
public:
    int trailingZeroes(int n) {
        int ans = 0;
        while(n)
        {
            int t = n / 5;
            ans += t;
            n = t;
        }
        return ans;
    }
};
原文地址:https://www.cnblogs.com/argenbarbie/p/5800403.html