问题描述:

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. vue键盘修饰符

    keyup事件 <input type='input' @keyup="keyEvent"> keyup.enter事件 <input type='input' ...

  2. vue对组件以数组方式赋值的问题

    当从后台直接调接口返回数据 直接将数组array赋值给定义的变量,会导致组件无法更改其它值,例如多选框,多选下拉框,会导致无法选中其它的值,也无法取消当前已赋值的选中项 data() { return ...

  3. MyEclipse上有main函数类运行报错:Editor does not contain a

    MyEclipse下有main函数类运行报错:Editor does not contain a main type?出现这种问题的原因是,该java文件   MyEclipse下有main函数类运行 ...

  4. Learn Python the hard way, ex40 字典,可爱的字典

    #!/usr/bin/python #coding:utf-8 cities ={'CA':'sf','MI':'dt','FL':'je'} #创建字典 cities['NY']='ny' #增加新 ...

  5. Show Me the Code

    最近在练习写Python代码,拥有150多道程序员面试题的LeetCode注重算法的实现,锻炼思维,还能在线测试代码的正确性,而Python练习册涉及到了Python实际的应用,锻炼解决问题的能力,托 ...

  6. 洛谷P2330 [SCOI2005]繁忙的都市——kruskal

    给一手链接 https://www.luogu.com.cn/problem/P2330 这道题实质就是最小生成树 TIPS:最小生成树不仅是整体权值最小,也是最大边最小 #include<cs ...

  7. JavaScript List

    function List() {    this.listSize = 0;    this.pos = 0;    this.dataSource = [];    this.clear = fu ...

  8. 基于dvwa环境下级别为low的SQL手工注入教程

    基于dvwa环境下级别为low的SQL手工注入教程: 首先是进入已搭建好的dvwa环境中去(一定要搭建好dvwa环境才能进行下面的操作),这可能会是一些初学者所面临的的第一个问题,比如我,曾为了寻找这 ...

  9. 详解Twitter开源分布式自增ID算法snowflake(附演算验证过程)

    详解Twitter开源分布式自增ID算法snowflake,附演算验证过程 2017年01月22日 14:44:40 url: http://blog.csdn.net/li396864285/art ...

  10. CentOS7安装MySQL5.7.20

    参考1:https://www.cnblogs.com/technology-huangyan/p/10484838.html 参考1:https://blog.csdn.net/aiyowei110 ...