题目描述:

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)解法的理解的更多相关文章

  1. LeetCode Factorial Trailing Zeroes Python

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

  2. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  4. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  5. Python3解leetcode Factorial Trailing Zeroes

    问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...

  6. LeetCode Factorial Trailing Zeroes (阶乘后缀零)

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

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

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

  8. 【LeetCode】172. Factorial Trailing Zeroes

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

  9. LeetCode Day4——Factorial Trailing Zeroes

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

随机推荐

  1. 链表倒数第n个节点

    找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...

  2. jquerymobile实例介绍

    [创建页面]   data-role="page" 是在浏览器中显示的页面..   data-theme="b"更换主题,有a和b两种   data-role= ...

  3. 网络基础一 交换机 路由器 OSI7层模型

    第1章 网络基础 1.1 网络的出现 解决计算机通讯的需求 实现计算机信息可以传递 1.2 主机之间实现通讯基本要求(三要素) ①. 需要在两台主机之间建立物理连接,物理连接的方式有网线 光纤线 wi ...

  4. Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException):

    用windows连接hadoop集群执行mapreduce任务的时候出现以下错误: org.apache.hadoop.security.AccessControlException:Permissi ...

  5. http 500错误怎么解决方法

    出现500错误的原因是很多的,一般来说,如果程序出错,那么在浏览器内会返回给用户一个友好的错误提示,统一称之为服务器500错误. 解决的方法就是您必须在http中能够正确的获得错误信息,方法为:请打开 ...

  6. Spring AOP分析(3) -- CglibAopProxy实现AOP

    上文探讨了应用JDK动态代理实现Spring AOP功能的方式,下面将继续探讨Spring AOP功能的另外一种实现方式 -- CGLIB. 首先,来看看类名CglibAopProxy,该类实现了两个 ...

  7. 微信支付——openid获取不到

    1.写微信支付遇到状况,通过wx.login获取code,然后向微信服务器获取openid,获取失败:{"errcode":40029,"errmsg":&qu ...

  8. C#中的静态成员和非静态成员

    C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢? 让我们来看看最直观的差别:使用了static 修饰符的方法为静态方法,反之则是非静态方法 ...

  9. [转载] ZooKeeper的Java客户端API

    转载自 http://www.cnblogs.com/ggjucheng/p/3370359.html http://zookeeper.apache.org/doc/trunk/javaExampl ...

  10. 使用Supervisor守护Python进程

    1.需求 现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,就需要使用到了Supervisor.Supervisor起到守护 ...