Leetcode #172 Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

Difficulty: Easy.

The trailing zeros depends on the numbers of 5 in (0, n). Also, we should notice the size of int.

class Solution {
public:
    int trailingZeroes(int n) {
        int ans = n/5;
        long long int num = 25;
        while(num<=n){
            ans = ans + n/num;
            num = num * 5;
        }
        return ans;
    }
};


你可能感兴趣的:(Leetcode)