leetcode 172. Factorial Trailing Zeroes

Description

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

Note: Your solution should be in logarithmic time complexity.

My solution

基本思路是:阶乘中的每个数分解因式, 总共加起来有几个5, 就对应总阶乘末尾有几个0.
最初想法为n/5, 比如10!末尾0的个数就是10/5, 直接统计里面5的个数即可. 10这个数字的0其实也是来源于10=5*2, 仍然可以视为一个5; 注意到个别例子, 如30!, 末尾有7个0, 30/5 = 6, 多出来的这个0来自于25=5*5中多了个5. 了解到这了解到这里, 思路便变成: 先求 k=log5(n) , 比如50!为例, 求的k=2, 于是最大的因子是25, 其中5*5多了一个5, 而50=5*5*2里面也有一个5. 故统计50/25=2即可. 同理若80!, 则还要在加上75=5*5*3这种情况, 实际上就是80/25=3.
上述思路代码如下,其中long long的引入是未保证不越界.(因为如果采用int测试用例中绝大多数正确,个别较大的数据错误,故基本可以确定是因为越界引起)

class Solution {
public:
    int trailingZeroes(int n) {
        int k=-1;
        unsigned long long m=0;
        unsigned long long val=1;
        while(val<=n){
            val*=5;
            k++;
            m+=n/val;
        }
        return (int)m;
    }
};

Discuss

discuss中思路基本和我类似, 但是实现方式略有不同. 注意到一个较为简洁的C++实现, 如下

Because from 1 to n, the number of 2 factors is always bigger than the number of 5 factors. So we only need to find the number of 5 factors among 1…n.

1st loop: 5, 10, 15, 20, 25, 30, ….

2nd loop: 25 50 ……
…..

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

    }
};

上述代码是My solution的简化版, 实际上基本原理是相同的, n/=5的方式大大简化了流程. 第1个loop只关心一个5的情况, 第二个loop只关心两个5的情况…
反观我的代码, 实在是绕圈了.

Reference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值