题目描述:

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

Note: Your solution should be in logarithmic time complexity.

解题思路:

这个题目给的评级是easy,其实只要想到要求n!中0的个数,能够得到0的只有:2,4,5,10,100.。。。而这里面又以5最为稀缺,所以说我们可以得出阶乘的最终结果中的0的数量等于因子中5的数量,比如说10,阶乘含两个0,因为有5,10;30含7个0,因为有5,10,15,20,25(25=5*5),30。

那么怎么求出有多少个呢?

简单的想法是:令\(f(n)\)表示\(n!\)有多少个0

\[f(n)=f(5)+2 \times f(5^2)+3 \times f(5^3)+\dots
\]

\[f(n)=n/5+f(n/5)
\]

递归版本:

class Solution:
# @return an integer
def trailingZeroes(self, n):
if n < 5:
return 0
else:
return n/5 + self.trailingZeroes(n/5)

迭代版本:

class Solution:
# @return an integer
def trailingZeroes(self, n):
counter = 0
while n:
counter += n / 5
n /= 5
return counter

【leetcode】Factorial Trailing Zeroes的更多相关文章

  1. 【leetcode】Factorial Trailing Zeroes(easy)

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

  2. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

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

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

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

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

  5. 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 ...

  6. ✡ 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 ...

  7. 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 ...

  8. leetcode:Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...

  9. Java [Leetcode 172]Factorial Trailing Zeroes

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

随机推荐

  1. Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)

    题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...

  2. JS实现网页批量下载文件,支持PC/手机

    //把下载链接放入集合里 var downloadData = new Array{"http://www.empli.com/data1.apk","http://ww ...

  3. HTML-如何让自己的网页标题处可以显示网站的logo?

    <link rel="Bookmark" href="/forum_images/ffico.png" /> <link rel=" ...

  4. ngBind ngBindTemplate ngBindHtml

    ng-bind: 只能绑定一个变量 在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{titleStr}}</p> 另一种是使用基于属 ...

  5. C++内存动态分配

    https://www.percona.com/blog/2012/07/05/impact-of-memory-allocators-on-mysql-performance/ https://su ...

  6. Android检测网络是否正常代码!

    在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...

  7. PHP中的变量与常量详解

    几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...

  8. sublime 编译运行C程序

    { "cmd": ["gcc", "${file}", "-o","${file_path}/${file_b ...

  9. DAY6 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单

    设置防火墙策略时,关于SSH:22访问权限,我们常常会设置服务器只接受某个固定IP(如公司IP)访问,但是当我们出差或在家情况需要登录服务器怎么办呢? 常用两种解决方案:1.通过VPN操作登录主机: ...

  10. ssh框架整合-NoClassDefFoundError-NoSuchMethodError-遁地龙卷风

    (-1)写在前面 spring2.0.struts1.2.hibernate3.0.myeclipse8.5.tomcat6.0,整合之中出现了很多问题,前几天忙着整理毕业论文的资料,时间腾出来了,总 ...