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.


给定整数n,找出小于n的数中,找出阶乘末尾为0的数的个数。




本题如果分别求1!,2!...n!,根本无法通过测试数据。
规律为:对于数字m!∈(0,n] ,如果m!末尾为0,那么必有1个因数为5和2。因此题目便转化为:
统计(0,n]之间,5约数个数和。




参考链接:


http://bookshadow.com/weblog/2014/12/30/leetcode-factorial-trailing-zeroes/
http://www.programcreek.com/2014/04/leetcode-factorial-trailing-zeroes-java/
http://www.danielbit.com/blog/puzzle/leetcode/leetcode-factorial-trailing-zeroes




实现代码:




public class Solution {
    public int TrailingZeroes(int n) {
        var c = 0;
        var factor = 5;
		var end = int.MaxValue / 5;
        while(n >= factor && factor != int.MaxValue){
            c += n/factor;
			if(factor > end){
				factor = int.MaxValue;
			}
			else{
				factor *= 5;
			}
        }
		
		return c;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值