LeetCode: Factorial Trailing Zeroes

Question:

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

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Reference:

http://blog.csdn.net/chenlei0630/article/details/42271019

http://bookshadow.com/weblog/2014/12/30/leetcode-factorial-trailing-zeroes/

Thoughts:

感谢 在线疯狂 博主的解释,此题只需要计算 n 中包含多少个5就可以了。

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

Author: Jerry Wu

Keep moving forward!

Leave a comment