172. Factorial Trailing Zeroes

Total Accepted: 50404
Total Submissions: 158693
Difficulty: Easy

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.

Java

1
2
3
4
5
6
7
8
9
10
11
public class Solution {
    public int trailingZeroes(int n) {
        int res = 0;
        while(n > 4)
        {
            res += n / 5;
            n /= 5;
        }
        return res;
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。