leetcode 172: Factorial Trailing Zeroes

时间:2015-02-03 11:23:03   收藏:0   阅读:253

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

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts  for adding this problem and creating all test cases.

注意时间复杂度

public class Solution {
    public int trailingZeroes(int n) {
        int num=0;
        int five = 1;
        //注意时间复杂度需要小,如果num =0; five =5;while(five<=n){five*=5; num +=n/five;}就time out 了
        while(five<=n/5){
        	five *=5;
        	num += n/five;        	
        }
        return num;
    }
}


原文:http://my.oschina.net/liusicong/blog/374698

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!