关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述:
Given an integer n, return the number of trailing zeroes in n!.
题目大意:
给定一个整数n,返回n!(n的阶乘)结果中后缀0的个数(如5!=120,则后缀中0的个数为1)。
解题思路:
int trailingZeroes(int n) {
return (n/>)?trailingZeroes(n/)+n/:;
}
首先这是LeetCode中时间复杂度为O(logn)的解法。
可以简单的知道,阶乘结果中后缀0的个数取决于n!中因数5的个数,因为5x2等于10,这样就出现了0,而因数中2的个数总是比5的个数多的,如5!=1x2x3x4x5,其中5x2得一个0,因数5的个数只有1个,因数2的个数由3个(2,4=2x2)。
重点是为什么上述代码可以求出阶乘中因数5的个数?
让我们举个阶乘61!的例子,一开始61/5=12,这说明1到61中有12个数可以被5整除(即具有因数5),分别是
5
而其他数相乘不会产生0,所以不用再考虑其他数了。
可以看出这12个数中都包含了因数5,但并不是每个数中都只包含1个因数5,如25=5x5,它包含两个因数5。所以,为了计算所有的因数5的个数,我们可以把这12个数做些改变,如
5 =5x1 =5x2
=5x3 =5x4
=5x5 =5x6
=5x7 =5x8
=5x9 =5x10
=5x11 =5x12
可以看出,我们从12个数中找到了12个因数5,剩下了1到12的序列。1到12的序列中也是有因数5的,那么1到12的序列中因数5的个数不就相当于找12!阶乘结果因数5的个数(即阶乘结果中后缀0的个数),这时候就重复递归,即代码中的trailingZeroes(n/5);
当n/5小于0,n小于5,自然就没有因数5了。
以上就是对LeetCode中时间复杂度为O(logn)的解法的理解,本文为本作者原创,转载请注明出处!
关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解的更多相关文章
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Python3解leetcode Factorial Trailing Zeroes
问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Linux学习(十二)mkpasswd、su、sudo、限制root远程登录
一.mkpasswd mkpasswd用来生成随机密码字符串.可以指定长度和特殊字符的长度: [root@ruanwenwu01 ~]# mkpasswd O7.alw5Wq [root@ruanwe ...
- c++の奇技淫巧
>如何用cmd编译c++?-m32究竟是什么操作?这究竟是道德的沦丧还是人性的泯灭,请收看今日的c++奇技淫巧 咳咳,扯远了(正经脸)主要是今天学了c++的一些编译技巧以及cmd的一些操作,总结 ...
- Jungle Roads(最小生成树)
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HTML超连接的使用
1,基本语法:<a href="" target="打开方式" name ="页面锚点名称">连接文字或者图片</a> ...
- HTTPS 建立连接的详细过程
HTTPS是在HTTP的基础上和ssl/tls证书结合起来的一种协议,保证了传输过程中的安全性,减少了被恶意劫持的可能.很好的解决了解决了http的三个缺点(被监听.被篡改.被伪装) 对称加密和非对称 ...
- TFS在项目中DevOps落地进程(下)
紧接上篇 TFS在项目中Devops落地进程(上) 再接着说TFS相关之前先插入一个番外篇,虽然跟TFS关系不大但跟DevOps关系很大,觉得有必要在此乱入一下. 番外篇--监控之Applicatio ...
- 移动端效果之LoadMore
写在前面 列表一直是展示数据的一个重要方式,在手机端的列表展示又和PC端展示不同,毕竟手机端主要靠滑.之前手机端之前一直使用的IScroll,但是IScroll本身其实有很多兼容性BUG,想改动一下需 ...
- spring aop使用
最近做一个数据库分离的功能,其中用到了spring aop,主要思路就是在service层的方法执行前根据注解(当然也可以根据方法名称,如果方法名称写的比较统一的话)来判断具体使用哪个库.所以想着再回 ...
- redis配置文件之复制
主从复制使用slaveof将Redis实例作为另一个Redis服务器的副本. 1) Redis复制是异步的,master可以配置成如果它连接的slave没有达到给定的数量,就停止接受写入.2) 如果断 ...
- HTML 使用jQuery选中复选框 简易版
<html><head> <meta charset="utf-8"> <script src="jquery-1.7. ...