码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode172]Factorial Trailing Zeroes

时间:2015-09-02 08:16:22      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:leetcode   math   

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;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

[Leetcode172]Factorial Trailing Zeroes

标签:leetcode   math   

原文地址:http://blog.csdn.net/sbitswc/article/details/48173297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!