问题描述:

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.

思路:

在n!中,若想在结果的结尾产生0,只能是5乘以双数、或者某个乘数结尾为0,如10,但10可视为5*2,20可以视为5*4.

综上要想找n!中有几个0,其实就是寻求在1到n这n个数中有几个5.

其中25=5*5,这需要视为2个5

代码目的就变成了寻找1到n这n个数中5的个数

代码:

 def trailingZeroes(self, n: int) -> int:
if n <= 0: return 0 return sum( (n//(5**j)) for j in range(1, int(math.log(n, 5)) + 1))

n//(5**j) ,当j=1时,就是寻找在1到n这n个数中有几个5

n//(5**j) ,当j=2时,就是寻找在1到n这n个数中有几个25(5*5)(在上一步计算中,25会被统计,一次,但由于25是5*5,内部含有两个5,因而在第二步需要再统计一次,即一共是算为2次)

依次类推

最后将结果累计相加,就可以计算出就是寻找在1到n这n个数中有几个5了

math.log(n, 5) 是求出以5为底,n的对数,然后向下取整,这个数就是j的最大值,因为如果j如果继续加1,那么5**j就会大于n,n//(5**j)恒为0,就没有计算意义了

Python3解leetcode Factorial Trailing Zeroes的更多相关文章

  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 O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

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

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

  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. 连接超时(connect timed out)和读取超时(Read timed out)

    设置连接超时和读取超时方法: RequestConfig config=RequestConfig.custom() .setConnectTimeout(10000) // 设置连接超时时间 10秒 ...

  2. redis为什么内存不宜过大

    redis的高性能.稳定性都是不用怀疑的,但如果redis塞入数据过多,内存过大,那如果出问题,那它可能会给我们的就是灾难性的. 1 主库宕机 主库宕机,常见的策略为“切主”.具体为从该集群剩余从库中 ...

  3. Parameter Initializations in Deep Learning

    全零初始化的问题: 在Linear Regression中,常用的参数初始化方式是全零,因为在做Gradient Descent的时候,各个参数会在输入的各个分量维度上各自更新.更新公式为: 而在Ne ...

  4. 关于VMware中的几个网络模式

    直接参考别人的: 写的已经很细致了: http://blog.csdn.net/yaoyaowugui/article/details/7422388 关键是看别人的几张图

  5. charles模拟弱网情况

    网络主要需要注意什么场景: 弱网功能测试 无网状态测试 网络切换测试 用户体验关注 下面我们使用charles测试弱网,针对不同网络下的测试 打开charles(抓包软件)  

  6. ThreadLocal和单例对象比较

    单例对象: 自始至终只有一个对象 当线程并发,每个线程需要自己独立的资源变量处理不同的业务时,单例对象远远不能满足需求 因此可以采用ThreadLocal模式 : 每个线程有自己独立的资源变量     ...

  7. c#批量插入

    一.创建一个用来测试的数据库和表 USE [Test] GO /****** Object: Table [dbo].[student] Script Date: 2019/4/11 15:38:59 ...

  8. C#面试 笔试题 六

    1.String str=new String("a")和String str = "a"有什么区别? String str = "a"; ...

  9. 20191108PHP数组查找练习

    <?php $arr=[20,30,60]; $new=array(22,"tom"=>33); echo $arr[2]; echo $new['tom']; for ...

  10. elasticsearch 深入 —— 地理位置

    地理位置 我们拿着纸质地图漫步城市的日子一去不返了.得益于智能手机,我们现在总是可以知道 自己所处的准确位置,也预料到网站会使用这些信息.我想知道从当前位置步行 5 分钟内可到的那些餐馆,对伦敦更大范 ...