172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量。
注意: 你的解决方案应为对数时间复杂度。
详见:https://leetcode.com/problems/factorial-trailing-zeroes/description/
Java实现:
N的阶乘可以分解为: 2的X次方,3的Y次方,4的K次方,5次Z方,.....的乘积。由于10 = 2 * 5,所以M只能和X和Z有关,每一对2和5相乘就可以得到一个10,于是M = MIN(X,Z),不难看出X大于Z,因为被2整除的频率比被5整除的频率高的多。所以可以把公式简化为M=Z。
方法一:
class Solution {
public int trailingZeroes(int n) {
int res=0;
while(n!=0){
n/=5;
res+=n;
}
return res;
}
}
方法二:超时
class Solution {
public int trailingZeroes(int n) {
int res=0;
for(int i=5;i<=n;++i){
int m=i;
while(m%5==0){
++res;
m/=5;
}
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4219878.html
172 Factorial Trailing Zeroes 阶乘后的零的更多相关文章
- ✡ 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 ...
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数
所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...
- 【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 ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- 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 ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
随机推荐
- Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡
<Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...
- Redis 命令行 常用总结
http://www.redis.cn/commands.html# 1 Keys * 列出所有的keys redis > keys * ) "s:0" ) "o: ...
- Android中的动画具体解释系列【2】——飞舞的蝴蝶
这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包含以下几个子类: AlphaAnimation ...
- 开发Nodejs(rest框架)版本的百度新闻系统--开发环境配置
项目介绍:配置好开发环境,制作前端百度新闻界面,后台开发成Nodejs版本,做成做成rest风格API形式搭载mysql,使用Bootstrap搭建后台页面,完成对新闻的增删改查功能,利用Ajax配合 ...
- oracle连接串的一种写法
我在.NET项目里访问oracle,向来是规规矩矩地这样写: DATA SOURCE=PDBGZFBC;PASSWORD=test;PERSIST SECURITY INFO=True;USER ID ...
- visio 2010 修改 默认字体 字号大小 方法[整理]
[转自]http://www.cnblogs.com/vegaliming/archive/2012/08/09/2630568.html 1.新建一个模具 2.将常用的图形放到这个模具中 3.对每个 ...
- Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find resource 'solrconfig.xml' in classpath or 'D:\solr\solr-7.2.1\server\solr\new_core'
\solr-7.2.1\server\solr\configsets\_default 下的conf 复制到: \solr-7.2.1\server\solr\new_core
- 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】
关键词:Android linux hrtimer 蜂鸣器 等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418 作 ...
- HDU1074 Doing Homework —— 状压DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (J ...
- 唯一性校验 impl 和 action
IMPL方法实现 // 这个方法是通过ID修改数据 如果得到结果大于0 表明结果有重复 如果得到结果小于0 表明结果正确 @Override public boolean checkVersion( ...