题目:

设计一个算法,计算出n阶乘中尾部零的个数

样例

11! = 39916800,因此应该返回 2

挑战

O(logN)的时间复杂度

解题:

常用方法:

也许你在编程之美中看到,通过求能够被2 整除和能够被5整除个数的最小值就是答案,或者直接求能够被5整除的个数就是答案<能够被5整除的数显然比较小>,但是在这里,java python都试了,结果都会出现运行超时或者越界的问题。

维基百科中有如下计算方法:

Java程序:

class Solution {
/*
* param n: As desciption
* return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
// write your code here
long count = 0;
for(long i=5;n/i>=1;i*=5){
count += n/i;
}
return count;
}
};

总耗时: 600 ms

时间好快的

Python程序:

class Solution:
# @param n a integer
# @return ans a integer
def trailingZeros(self, n):
count = 0
i = 5
while n/i>=1:
count +=n/i
i = i * 5
return count

总耗时: 98 ms

在维基百科下面还有下面的方法:

Java程序:

class Solution {
/*
* param n: As desciption
* return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
// write your code here
long q = n;
long count = 0;
while (q!=0){
count +=q/5;
q = q/5;
}
return count;
}
};

总耗时: 583 ms

Python程序:

class Solution:
# @param n a integer
# @return ans a integer
def trailingZeros(self, n):
count = 0
p = n
while p!=0:
p = p/5
count +=p
return count

总耗时: 104 ms

lintcode :Trailing Zeros 尾部的零的更多相关文章

  1. LintCode——尾部的零

    尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...

  2. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  3. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

  4. codewars--js--Number of trailing zeros of N!

    问题描述: Write a program that will calculate the number of trailing zeros in a factorial of a given num ...

  5. Trailing Zeros

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  6. [LeetCode] Factorial Trailing Zeros

    Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...

  7. [Algorithm] 2. Trailing Zeros

    Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...

  8. LintCode #2 尾部的零

    计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...

  9. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

随机推荐

  1. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  2. http 错误编号大全(转)

      状态行包含HTTP版本.状态代码.与状态代码对应的简短说明信息.在大多数情况下,除了Content-Type之外的所有应答头都是可选的.但Content-Type是必需的,它描述的是后面文档的MI ...

  3. Keil uVision4 代码编辑器中文字符乱码问题

    MDK-ARM 使用中一直有个很纠结的问题,中文字符支持不好. 比如写代码注释,使用中文删除字符就会只删除一半问题.复制粘贴代码中间有中文就会出现乱码问题. 想过换IAR,新学个IDE也麻烦,上面的问 ...

  4. Linux磁盘与文件系统管理

    df df(disk free) 功能说明:显示磁盘的相关信息.语 法:df [-ahHiklmPT][--block-size=<区块大小>][-t <文件系统类型>][-x ...

  5. mdelay,udelay,msleep区别

    delay函数是忙则等待,占用CPU时间:而sleep函数使调用的进程进行休眠. udelay引用头文件/include/asm-***/delay.h,mdelay和ndelay则引用/includ ...

  6. php protected只能被继承,不可以在实例中调用,parent::调用父类(子类函数的重载对父类的函数没有影响)

    <?php class a { private function fun1(){ echo 'a1'; } //protected 可以被继承,但是只能在子类中使用,不能被实例化调用 prote ...

  7. (C学习基础)一,CMD的使用

    以后考研要考数据结构,所以系统的整理一下C学习笔记.这里开发用VS2013  CMD资料在这里下载 首先就是DOS系统的CMD的使用,是不是觉得会命令行语句的人特别牛呢,有时候使用命令行总是事半功倍, ...

  8. [转]- Winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    转自:http://heisetoufa.iteye.com/blog/382684 第一种方法: 用委托,Form2和Form3是同一组 Form2  using System; using Sys ...

  9. mutable和volatile关键字

    1.mutable 在C++中,mutable是为了突破const的限制而设置的.被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其 ...

  10. 利用getchar()消除多余字符数据(主要是“回车”)