Given an integer n, return the number of trailing zeroes in n!.

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.

Note: Your solution should be in logarithmic time complexity.

思路是因为Because all trailing 0 is from factors 5 * 2, 然后2这个因子总是足够的, 比如2, 4, 6, 每两个就有个2的因子, 然后我们只需要数5的个数即可, 另外要反复循环直到没有5为止.

比如1,2,3,4,5,6,7,8,9,10

发现 1-4: 0

5-9: 1

    10-14: 2

Code(O(lgn))

class Solution:
def FactorialZeroTail(self, n):
return 0 if n == 0 else n//5 + self.FactorialZeroTail(n//5)

[LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math的更多相关文章

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

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

  2. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

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

  3. Java for LeetCode 172 Factorial Trailing Zeroes

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

  4. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

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

  5. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

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

  6. Java [Leetcode 172]Factorial Trailing Zeroes

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

  7. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

  8. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  9. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

随机推荐

  1. 【小程序+ thinkphp5】 获取微信运动数据

    配置.请参看上篇文章.这里直接上代码 PHP 代码: //获取微信运动数据: public function test(){ $code = input("code"); $sig ...

  2. c++ 函数的默认参数

    /** * @file test.cpp * @author chenjiashou(chenjiashou@baidu.com) * @date 2017/08/20 15:54:27 * @ver ...

  3. 【问题记录系列】the resource is not on the build path of a java project

    在eclipse中新建了一个maven项目搭建Spring源码阅读环境,创建一个bean生产getter和setter方法的时候报错“the resource is not on the build ...

  4. 【ORACLE 】 ORA-00031 标记要删去的会话(解决)

    在使用Oracle的过程中,会有使用了锁(for update)但又忘记释放锁的情况.这是就需要用到KILL语句了.(如果不知道KILL语句怎么用,可参考: http://www.cnblogs.co ...

  5. js 的空值判断程序

    function empty(v){ switch (typeof v){ case 'undefined' : return true; case 'string' : if($.trim(v).l ...

  6. GoldenGate的监控

    1.进入GoldenGate安装目录,运行GGSCI,然后使用info all查看整体的运行状况 GGSCI (aix212) 1> info all Program Status Group ...

  7. Elasticsearch学习之深入搜索二 --- 搜索底层原理剖析

    1. 普通match如何转换为term+should { "match": { "title": "java elasticsearch"} ...

  8. 问题记录 为ubuntu16.04添加windows字体(解决JIRA图表乱码的问题)

    最近遇到了JIRA在新的ubuntu机器上图表的中文无法正确显示的问题,解决的方法是,为ubuntu安装中文字体,我们选择把windows上的字体复制到ubuntu上来安装的方法,步骤如下: 从win ...

  9. kettle中使用JavaScript的一个例子

    最近在使用kettle的时候遇到一个问题,需要对输入的一个字段进行格式化,逻辑比较复杂(需要做替换掉指定的字符串,然后将数字部分不足四位的数前边补0等操作),kettle中没有提供直接的插件来支持复杂 ...

  10. markdown公式编辑参考

    原文作者,https://www.cnblogs.com/q735613050/p/7253073.html