leetcode 172. Factorial Trailing Zeroes

来源:互联网 发布:淘宝网红直播推广费用 编辑:程序博客网 时间:2024/05/20 05:44

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

  • leetcode 172
原创粉丝点击