原题地址:

https://oj.leetcode.com/problems/factorial-trailing-zeroes/

题目内容:

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

Note: Your solution should be in logarithmic time complexity.

方法:

数学原理很简单,稍微讲一下

我们知道,一堆数相乘出了0,除了有0之外,需要一个2,5数对。比如4 × 5,可以分解为2 × 2 × 5,有一个2,5数对,所以有1个0。推而广之,一堆数连乘,能因式分解出几个2,5数对就有几个0。

由于在阶乘中,分解出的2肯定比5多,(要证明吗?稍微证一下。。2的倍数,也就是全体偶数在一组阶乘中肯定比5的倍数多,而只有2和5的倍数能因式分解出2,5数对来组合,因此,每一个2,5数对和数字5一一对应),因此,实际上给了我们n,我们需要找出,从1到n这个区间中,能分解出几个5。

数学原理讲完了,讲算法。

先想想,我们如何求1到n的所有5的倍数?答案很简单,n/5就可以了,因为每5个数就会有一个5的倍数。我们先把所有5的倍数个数加到结果中先。

可是这样还远远不够,因为25中能分解出2个5,而所有25的倍数都能分解出两个5,以此类推。

但我们离答案已经很近了。n/25是区间内所有25的倍数,由于25的倍数在第一轮5的倍数中,已经加了一个5,因此,这一轮也只需要加一次就行了。加上所有25的倍数的个数到结果中去,以此类推。

最后需要注意一点:乘法溢出问题。5的13次方是末尾,14次方就溢出了。

具体代码:

Python就三行,我去

class Solution:
# @return an integer
def trailingZeroes(self, n):
l = [5 ** i for i in range(1,14)]
q = [n / key for key in l]
return sum(q)

C++有点多

class Solution {
private:
vector<int> dict;
public:
Solution () {
int start = 5;
int border = 13;
for (int i = 0; i < 13; i ++) {
dict.push_back(start);
start *= 5;
}
} int trailingZeroes(int n) {
int res = 0,i = 0;
int p;
while (i < dict.size() && (p = n / dict[i ++]) > 0) {
res += p;
}
return res;
}
};

复杂度的要求毫无疑问是满足的,常数次,比log都好。

【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告的更多相关文章

  1. 【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 递归 循环 日期 题目描述 Given an integer ...

  2. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  3. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  4. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  5. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  6. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  7. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  8. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【原创】leetCodeOj --- Sliding Window Maximum 解题报告

    天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...

随机推荐

  1. 第m个全排列

    #include<stdio.h> #include<string.h> int flag,n,m; ],sum,vis[]; void dfs(int k) { ) retu ...

  2. Latin1的所有字符编码

    ISO-8859-1 (ISO Latin 1) Character Encoding Contents The characters at a glance Character codes and ...

  3. uva 11355(极角计算)

    传送门:Cool Points 题意:给一个圆心为原点的圆和一些线段,问所有线段两端点与圆心连线构成的角度总和占总360度的百分比. 分析:首先将所有线段的两端点变成极角,然后排序(范围[-PI,PI ...

  4. swift 笔记2

    swift交流群:342581988,欢迎增加. 今天真郁闷啊,把mac升级到10.10了.如今好了,曾经的程序都跑不了了.哎,不说了,让我郁闷会再. 说说条件推断吧,事实上这些基本的语法大家都知道肯 ...

  5. C3P0

    c3p0详细配置 官方文档 :  http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-con ...

  6. python语言学习4——使用文本编辑器

    在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个文件,这样, ...

  7. Liftoff Software | Next stop, innovation

    Liftoff Software | Next stop, innovation Previous Next Gate One 1.1 Now Available Submitted by Dan M ...

  8. WebKit爬虫

    https://github.com/emyller/webkitcrawler 一个开源的项目,可以快速入门. http://spiderformysql.com/ http://crawl.gro ...

  9. Android编程 获取网络连接状态 及调用网络配置界面

    获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...

  10. php截取等长UFT8中英文混合字串

    由于需要,想实现“php截取等长UFT8中英文混合字串”,可是网上找了很多代码不是有乱码就是不能实现等长(以一个中文长度为单位,两个英文字母算一个长度,如‘等长’长度为2,‘UTF8’长度也是2). ...