首页 > 其他 > 详细

[LeetCode] Factorial Trailing Zeroes

时间:2015-07-13 22:10:02      阅读:228      评论:0      收藏:0      [点我收藏+]

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、扩展:

[LeetCode] Factorial Trailing Zeroes

原文:http://www.cnblogs.com/maydow/p/4643838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!