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 ...
随机推荐
- Android开发之计算器(一)界面设计之activity_main布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- C# Winfrom小黄鸡功能调用
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能. 接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块, Winfrom版 后台界面代码 ...
- ios6 处理内存警告
iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...
- [MySql] 设置了UTF8,中文存数据库中仍然出现问号
运行命令:SHOW VARIABLES LIKE 'character_set_%'; 结果 'character_set_client', 'utf8' 'character_set_connect ...
- VBS基础篇 - FileSystemObject对象
文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject FSO包含的常见对象有: ...
- Daily Scrum 11.6
摘要:在本次meeting时,所有代码的修改工作已经接近尾声,接下来是进行的就是单元测试以及进行alpha版本的改进.本次的Task列表如下: Task列表 出席人员 Today's Task Tom ...
- DB天气app冲刺二阶段第十天
昨天困到不行了 所以就写了那么几句..所以今天好好写写了要.. 今天的收获了一个很重要的问题 就还是api接口的事情,以前的那个接口虽然能用但是总是不稳定,今天由决定百度的一下然后就发现了一个很好用的 ...
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):4 同步编码
本文档描述Video Toolbox实现同步编码的办法. Video Toolbox在头文件描述了编码方式为异步,实际开发中也确实为异步. This function may be called as ...
- SQL SERVER 強制指定使用索引 -转载 只为学习
今天很高兴 ,有学会了一种数据库优化的方式,哈哈 今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB', ...
- 解决在ubuntu下requests 无法找到模块packages
我明明用pip install requests安装成功了,但是依然报下面的错 错误1 requests.packages.urllib3.disable_warnings()AttributeErr ...