原题地址:

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. [Android]Volley源代码分析(店)应用

    通过前面的谈话,我相信你有Volley有了一定的了解了原理.本章将给出一些我们的应用程序都可以在样品中直接使用,第一样品是 NetworkImageView类,事实上NetworkImageView顾 ...

  2. poj 3270 更换使用

    1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...

  3. MySQL JDBC事务处理、封装JDBC工具类

    MySQL数据库学习笔记(十)----JDBC事务处理.封装JDBC工具类 一.JDBC事务处理: 我们已经知道,事务的概念即:所有的操作要么同时成功,要么同时失败.在MySQL中提供了Commit. ...

  4. oschina 建站系统

    建站系统 分类网站程序(9) 众筹平台(2) 团购网站系统(14) 开源轻博客系统(8) 开源博客系统(279) 视频网站系统(9) 开源微博工具(93) 论坛系统BBS(129) 建站系统CMS(5 ...

  5. Domain Model(领域模型)

    Domain Model(领域模型) 上一篇:<DDD 领域驱动设计-如何 DDD?> 开源地址:https://github.com/yuezhongxin/CNBlogs.Apply. ...

  6. Codeforces 452A Eevee

    #include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...

  7. JAVA card 应用开发(三) 把APPLET(CAP文件)装载到卡片

    依据前面两篇博文.我们能够在Eclipse上建立好Applet.而且能够有多个AID.能够选择不同的应用. 那么,以上我们都是基于模拟环境的逻辑,实际上有些函数接口是须要实际的环境.就是说我们须要把A ...

  8. hdu3966(树链剖分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:一颗树上,每个点有权值,定义三种操作: 1)I操作表示从a到b节点之间的节点都加上一个值 ...

  9. 风起看云涌,叶落品人生 - Google 搜索

    风起看云涌,叶落品人生 - Google 搜索 风起看云涌,叶落品人生

  10. 使用SSIS对Dynamics CRM 系统进行数据迁移

    嗨,各位.近期项目一直都非常忙,而且自己也一直在思考职业发展的问题,所以有非常长一段时间没静下心写几篇Blog了.近期我參与的项目是Dynamics CRM 2011 到 Dynamics CRM 2 ...