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. TCP端口号范围及分类

    https://blog.csdn.net/my_heart_/article/details/52601924 端口号的范围是从1-65535 端口的概念:  在网络技术中,端口(Port)大致有两 ...

  2. 【Python系列】Python3获取控制台输入

    """ 接收控制台的输入 How old are you? 18 How tall are you ? 180 How much do you weigh? 50 So ...

  3. 字符串处理总结之一(C#String类)

    C#(静态String类) C#中提供了比较全面的字符串处理方法,很多函数都进行了封装为我们的编程工作提供了很大的便利.System.String是最常用的字符串操作类,可以帮助开发者完成绝大部分的字 ...

  4. Centos重新启动网络配置文件,/etc/resolv.conf被覆盖或清空问题解决

    Centos在执行命令 yum update时报错如下: Could not get metalink https://mirrors.fedoraproject.org/metalink?repo= ...

  5. C陷阱与缺陷读书笔记

    2.1理解函数声明 这一章仔细分析了(*(void(*)())0)();这条语句的含义,并且提到了typedef的一种函数指针类型定义的用法. 我们经常用到的typedef用法是用于指定结构体的类型, ...

  6. sencha touch 在线实战培训 第一期 第一节

    经过忙碌的准备,终于在2013.12.28晚上8点开了第一节课. 第一次讲课有些小紧张,讲的内容也比较基础,不过算是开了一个好头. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内 ...

  7. FileInputStream 和 FileOutputStream

    简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...

  8. Mongoose学习参考资料

    我们学习node操作mongodb数据库时,可能经常要用到MongoDB 数据库的模型工具Mongoose,下面是我整理的一些学习资料: 番外篇之——使用 Mongoose:https://githu ...

  9. python中filter(),map()和reduce()的用法及区别

    先看filter()方法 print(list(filter(lambda n : n % 2 == 1, range(20))))# 结果 [1, 3, 5, 7, 9, 11, 13, 15, 1 ...

  10. MyEclipse无法创建servers视图:Could not create the view: An unexpected exception was thrown

    今天上班刚打开MyEclipse,就发现servers视图无法打开了,显示:Could not create the view: An unexpected exception was thrown. ...