题目描述:

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. C++ 随机生成一个(0,1)之间的小数

    double p; ]; memset(s,,sizeof(s)); s[]='; s[]='.'; ;i<;i++) { s[i]=rand()%+'; } p=atof(s); cout & ...

  2. robotframework自动化系列:修改流程

    在上一小节中,新增流程操作已经完成.那么接下来就是修改的流程操作了,对于一个页面的修改如何操作呢? 针对修改修改操作是在一个已经存在的账号直接修改还是新添加一个账号再修改呢?从用例设计健壮性出发,我们 ...

  3. Unity跨平台C/CPP动态库编译---可靠UDP网络库kcp基于CMake的各平台构建实践

    1.为什么需要动态库 a)提供原生代码(native code)的支持,也叫原生插件,但是我实践的是c/cpp跨平台动态库,这里不具体涉及安卓平台java库和ios平台的objectc库构建. b)某 ...

  4. OpenTSDB-Writing Data

    Writing Data You may want to jump right in and start throwing data into your TSD, but to really take ...

  5. Mybatis3 快速入门

    Mybatis3 快速入门 目前常见的持久层java框架有Hibernate,Mybatis,SpringData.笔者比较喜欢用SpringData.Hibernate 和 Mybatis 也经常用 ...

  6. 调试利器:SSH隧道

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 本文作者:ivweb 吴浩麟 原文出处:IVWEB社区 未经同意,禁止转载 在开发微信公众号或小程序的时候,由于微信平台规则的限制,部分接口需 ...

  7. 一个高性能异步socket封装库的实现思路 (c#)

    前言 socket是软件之间通讯最常用的一种方式.c#实现socket通讯有很多中方法,其中效率最高就是异步通讯. 异步通讯实际是利用windows完成端口(IOCP)来处理的,关于完成端口实现原理, ...

  8. MeshRenderer组件及相关API

    MeshRenderer:网格过滤器,用于"渲染"显示模型. Cast Shodows:是否投射阴影.(on:开.off:关) Receive Shodows:是否接收阴影. Ma ...

  9. jquery.uploadify+spring mvc实现上传图片

    一.前端页面 1.下载jquery.uploadify 去uploadify官网(http://www.uploadify.com/download/)下载压缩包,解压后放在如下路径: 2.html结 ...

  10. RedHat升级Python到2.7.6

    今天本来想研究一下Python  paramiko模块,安装安装 paramiko-1.10.1.tar.gz的时候报错,!看了一下虚拟机RedHat中的python,发现还是原生的2.4.3,所以决 ...