172. Factorial Trailing Zeroes
题目:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
链接: http://leetcode.com/problems/factorial-trailing-zeroes/
题解:
求n!里有多少个0。其实主要就是看有多少个5,有多少个5就有多少个0,这样我们就可以用一个while循环来搞定。
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int trailingZeroes(int n) {
if(n <= 0)
return 0; int res = 0;
while(n > 0) {
res += n / 5;
n /= 5;
} return res;
}
}
二刷:
找到在n里有多少个5,就可以得到结果。
Java:
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int trailingZeroes(int n) {
if (n < 5) {
return 0;
}
int count = 0;
while (n > 0) {
count += n / 5;
n /= 5;
}
return count;
}
}
三刷:
题目可以转化为,求n!中含有多少个5。如何计算一个数里面有多少个5呢?我们可以用以下公式:
count = n / 5 + n / 25 + n / 125 + ....
就是用n除5来取得一开始的基数,当遇到5的倍数的时候,我们也要作相应的增加, 转换为循环的话我们可以先计算单个5的个数 n / 5,然后 n /= 5来计算 25的个数,然后再继续。最后返回count.
Java:
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int trailingZeroes(int n) {
if (n < 5) {
return 0;
}
int count = 0;
while (n > 0) {
count += n / 5;
n /= 5;
}
return count;
}
}
Reference:
https://leetcode.com/discuss/62976/my-python-solution-in-o-1-space-o-logn-time
https://leetcode.com/discuss/19847/simple-c-c-solution-with-detailed-explaination
http://15838341661-139-com.iteye.com/blog/1883889
172. Factorial Trailing Zeroes的更多相关文章
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 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 ...
- ✡ 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 ...
- 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 ...
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 【一天一道LeetCode】#172. Factorial Trailing Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
随机推荐
- Linux设备总线
kobject和kset是Linux设备模型中最基本的元素,其中,kset是同种类型kobject对象的集合.每个在内核中注册的kobject对象都对于sysfs文件系统中的一个目录.下面是自己花的一 ...
- 随机森林之Bagging法
摘要:在随机森林介绍中提到了Bagging方法,这里就具体的学习下bagging方法. Bagging方法是一个统计重采样的技术,它的基础是Bootstrap.基本思想是:利用Bootstrap方法重 ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- Mysql 数据库安装配置
MySQL的多种安装方法 在当今的互联网企业,Mysql数据服务几乎都是运行在LINUX系统操作系统上,当然你也可以在WINDOWS.UNIX等商业操作系统上运行. 但是一般企业都会采用LNMP.LA ...
- L012-oldboy-mysql-dba-lesson12
L012-oldboy-mysql-dba-lesson12 graphite监控mysql可以达到秒级别 Zabbix for mysql NagiosXI daemontools linux ...
- STL merge的实现细节
//std::merge的两个版本 template<class InputIt1, class InputIt2, class OutputIt> //First version Out ...
- Node.Js之【Stream 之复制文件问题】
var fs = require('fs'), path = require('path'), out = process.stdout; var filePath = 'F:/IMG_0221.MO ...
- 常用mysql命令大全
常用的MySQL命令大全 一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再 ...
- Mindjet.MindManager“参数错误”解决办法,适用于9.0、10.0和14.0
MindManager出14.0版本了,但是在应用个别模板的时候会提示“参数错误”,然后自动关闭. 解决办法: 如果是win7系统,可以进入C:\Users\(用户名) \AppData\Loca ...
- CCNP第三天 EIGRP综合实验
实验题如图所示:其中R2连R3 R5为快速以太网线,其他均为串线,帧中继默认是富曼斯(全连网状结构),即所有接入的路由之间的PVC都已经打通,所有 要关闭R5和R8的逆向arp功能,来手工配置R5到 ...