Trailing zero in factorial

1)找5的因子,因为2肯定够;

2)通过不断除5,可以把5的因子数都找出来;

code

class Solution {
public:
    // param n : description of n
    // return: description of return 
    long long trailingZeros(long long n) {
        long long sum = 0;
        while (n != 0) {
            sum += n / 5;
            n /= 5;
        }
        return sum;
    }
};

results matching ""

    No results matching ""