[Leetcode172]Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.


solution:

zero comes from 2*5, and number of 2 is less than 5. So we can only count the number of 5 contained in n!.

public int trailingZeroes(int n) {
        if(n<5) return 0;
        int count = 0;
        while(n/5 !=0){
            n/=5;
            count +=n;
        }
        return count;
    }


你可能感兴趣的:(LeetCode,Math)