原题地址:

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. sqlserver 自学笔记之 常量,变量及函数

    (1)全局变量. 输入以下sql语句,根据查询结果,了解sqlserver全局变量的含义 select @@VERSIONselect @@CONNECTIONSselect @@CURSOR_ROW ...

  2. js下读取input中的value值

    很多人(包括我),总想像以前操作js一样,读取到input中的值:document.getElementById('').value; 结果事实证明这样读到得是null. eval(document. ...

  3. Eclipse添加和查看书签

    添加书签: 在编辑框左边栏右击 > Add Bookmark > 编辑书签名称(可以直接使用默认名称)> OK 查看书签: Window > Show View > Ot ...

  4. Python语法

  5. Apache+windows server2008 外网访问配置

    之前在一个服务器上部署一个apache网站,在局域网内都可以访问,但是外网始终访问不了,经常多次谷歌,把解决方案总结出来. 下面就默认部署apache自带的网站.系统:windows server20 ...

  6. 调用ShellExecute需要头文件

    调用ShellExecute需要头文件 #include   "windows.h " #include   "shellapi.h "

  7. 基于libevent, libuv和android Looper不断演进socket编程 - 走向架构师之路 - 博客频道 - CSDN.NET

    基于libevent, libuv和android Looper不断演进socket编程 - 走向架构师之路 - 博客频道 - CSDN.NET 基于libevent, libuv和android L ...

  8. 基于Grunt的版本号构建系统新手教程

    作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...

  9. 秒杀多线程第二篇 多线程第一次亲热接触 CreateThread与_beginthreadex本质差别

    本文将带领你与多线程作第一次亲热接触,并深入分析CreateThread与_beginthreadex的本质差别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...

  10. NGUI使用教程(3) 使用外部图片制作Atlas(图集)

    在实际操作之前有几个概念先弄清一下 Atlas:图集,把美术给你提供的素材,用 NGUI 的 Atlas Maker 工具.合成一张图片(准确的说,还同一时候生成了prefab.mat ). Spri ...