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 ...
随机推荐
- 杂项一之js,<select>标签
一.在aspx页面中实现 修改与删除页面的跳转 前台js部分: 在上部的js部分中写,根据传过来的id,来经行页面的跳转,并把id传过去 js部分就是实现了一个页面跳转的功能 (还有确认框confir ...
- js数组的splice方法
w3school文章链接:http://www.w3school.com.cn/jsref/jsref_splice.asp splice:拼接,结合. splice()方法向数组添加/删除元素,返回 ...
- ActiveMQ之JMSReplyTo
在下面的例子中,首先创建两个Queue,发送者给一个Queue发送,接收者接收到消息之后给另一个Queue回复一个Message,然后再创建一个消费者来接受所回复的消息.import javax.jm ...
- c位段
假如程序表示四盏灯的开关状态灯只有开或关两种状态所以用1和0就可以表示为了节省内存就用一个二进制位表示一盏灯这里就定义位域用 a b c d 各表示一盏 这里定义时注意选用无符号类型位域允许用各种格式 ...
- oracle-11g创建用户名的时候默认区分大小写
oracle11g-11.2.0.3.0 - 64bit oracle-11g创建用户名的时候默认区分大小写 设置不区分大小写: alter system set sec_case_sensitive ...
- ngnix 下配置多域名跳转 配置方法
自己申请了多个域名,统一使用 http://goipc.cn/ 格式访问网站 server_name goipc.cn ; server_name www.goipc.cn ; server_name ...
- 【软件工程-Teamwork 3】团队角色分配和团队贡献分分配规则
Part 1 团队角色分配 1.人员分配概要: Project Manager:1名 / Developer:4名 / Test: 1名 2.具体人员分配及职责: Project Manager(PM ...
- C# ASP .NET WEB方向和WPF方向,我该如何去选择
一个2012年南航毕业学软件的学生,该如何去选择我的职业方向? 2011年11分月份,我被老师介绍在南京珠江路华丽国际大厦工作,开发一个大型国际物流平台,公司的开发人员比较少,设计网站的是高校的老师, ...
- OC 加密
//MD5加密的结果为128位的二进制数. //所以有128 / 8 = 16字节(8位一个字节). //每八位表示两个16进制数. //MD5 有32个16进制数. //语言层次的所有摘要算法步骤类 ...
- matlab查找回车字符
Hi all, I would like to read the data all at once with: `file_text = fread(fid, inf, 'uint8=>char ...