[LeetCode] Factorial Trailing Zeroes

Question:

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

1、题型分类:

2、思路:寻找n!后面的0的个数,即有多少个2*5,从而需要寻找里面总共有多少个2和多少个5,2肯定比5多,则只要找出5的个数即可。n/5是从n/5到n中5的倍数的个数

3、时间复杂度:

4、代码:

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

 

5、优化:

6、扩展:

posted @ 2015-07-13 20:25  Maydow  阅读(113)  评论(0编辑  收藏  举报