leetcode_Factorial Trailing Zeroes

描述:

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

Note: Your solution should be in logarithmic time complexity.

思路:

一般来讲,.5*2  .5 *4  .5*8 .....可以获得末位是0的情况,同理25*2 25*4 ...所以仅需递归地求出从1到n中,5的个数,25的个数,125的个数,625的个数。。。即可

代码:

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


你可能感兴趣的:(factorial,z,trailing)