lintcode :Trailing Zeros 尾部的零
题目:
设计一个算法,计算出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 尾部的零的更多相关文章
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
- 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 ...
- Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- [LeetCode] Factorial Trailing Zeros
Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...
- [Algorithm] 2. Trailing Zeros
Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...
- LintCode #2 尾部的零
计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
随机推荐
- PHP中include和require绝对路径、相对路径问题
在写PHP程序时,经常要用到include或require包含其他文件,但是各文件里包含的文件多了之后,就会产生路径问题. 如下目录: <web>(网站根目录) ├<A>文件夹 ...
- 客户端访问WebService和PageMethod
客户端访问WebService 客户端访问WebService和后台访问WebService没什么不同,注意的地方是要在ScriptManager中添加 <Services> ...
- python学习2——数据类型
1. python是强类型 动态类型的语言,动态类型表明它可以在声明变量的时候,不必指定数据类型,强类型规定了它不能容忍隐式类型转换 2. python中的不可变类型有:int,string,tupl ...
- WPF编译时提示“...不包含适合于入口点的静态‘Main’方法 ...”
今天看了一下wpf的Application类方面的知识,一个windows应用程序由一个Application类的实例表示,该类跟踪在应用程序中打开的所有窗口,决定何时关闭应用程序(属性 Shutdo ...
- XAML 概述一
XAML的全称是Extensible Application Markup Language,就是我们所说的可扩展应用程序标记语言.XAML可以应用到许多不同领域,但主要用于构建用户界面. XAML是 ...
- 解决This system is not registered with RHN
Redhat之所以会出现这个错误是因为没有注册RHN,我们只需要更新一下yum的源就可以了.使用命令 cd /etc/yum.repos.d/ 进入yum的配置目录. 在终端中输入 wget ht ...
- 【h5-egret】如何快速开发一个小游戏
1.环境搭建 安装教程传送门:http://edn.egret.com/cn/index.php?g=&m=article&a=index&id=207&terms1_ ...
- SharePoint 2010/SharePoint 2013 Custom Action: 基于Site Collection 滚动文字的通知.
应用场景: 有时候我们的站点需要在每个页面实现滚动文字的通知,怎么在不修改Master Page的情况下实现这个功能?我们可以使用Javascript 和 Custom Action 来实现. 创建一 ...
- iOS 下拉菜单 FFDropDownMenu自定义下拉菜单样式实战-b
Demo地址:https://github.com/chenfanfang/CollectionsOfExampleFFDropDownMenu框架地址:https://github.com/chen ...
- [algorithm]求最长公共子序列问题
最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...