题目描述:

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. HDU 6092 Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. css超过一定长度显示省略号

    overflow: hidden; white-space: nowrap; text-overflow: ellipsis;

  3. Android 开发笔记___复选框__checkbox

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  4. svg snap 笔记

    路径中的字母,大写相对于左上角绝对定位,小写相对定位  M110,95,95,110M115,100,100,115   pattern 类似于图片拼贴,可以把指定位置的图案用来填充 var patt ...

  5. MQTT——发布报文

    发布报文的知识点并不难,只是多.看过前面几章的读者们应该或多或少都认识服务质量QOS.发布报文跟他的联系最紧的.我们也清楚订阅报文里面虽然也有用到QOS,但是他却没有更进一步的联系.往下看就知道是什么 ...

  6. linux下mysql启动出错

    1.刚安装完就启动出错,是因为没有开msql服务,开启即可,service mysql start 2.MySQL: mysql is not running but lock exists rm / ...

  7. C#删除区域实现透明

    最近在搞一个图形图像的项目.不知道经理为什么选择了C#语言,但还是要做,呵呵. 在期间出现一个比较难解决的问题如下: 删除当前图层的指定区域用来显示下面图层在这个区域的图像,相当于PS蒙版层的效果. ...

  8. 《天书夜读:从汇编语言到windows内核编程》一 汇编指令与C语言

    1. Debug模式下,VC++6.0下断点运行,按CTRL+F11可查看汇编代码:另外可以用cl /c /FAs YourCppFile.cpp命令行在同目录生成YourCppFile.asm汇编文 ...

  9. requests和BeautifulSoup

    一:Requests库 Requests is an elegant and simple HTTP library for Python, built for human beings. 1.安装 ...

  10. javascript 的继承

    我们的JavaScript比较特别了,主要通过原型链实现继承的. 下面介绍各种实现继承的方式:原型链继承,借用构造函数,组合继承,原型式继承,寄生式继承,寄生组合式继承. 二.实现继承方式 1.原型链 ...