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 ...
随机推荐
- Win7 Windows Update更新的文件默认在哪个位置
C:\Windows\SoftwareDistribution\download
- STM32 USB复合设备编写
目的 完成一个CDC + MSC的复合USB设备 可以方便在CDC,MSC,复合设备三者间切换 可移植性强 预备知识 cube中USB只有两个入口. main函数中的MX_USB_DEVICE_Ini ...
- JMeter Web测试快速入门教程
学习前的准备 学习本教程前,你的电脑上至少应该有Apache JMeter这款软件.如果你没有,点击此处下载. 当你点进去后,你会发现它是一个依赖Java的软件 因此如果你电脑上没有Java环境,你应 ...
- NPOI解析Excel
园子的文章: http://www.cnblogs.com/csqb-511612371/category/654604.html 关键就是如何解析Excel表头,特别是合并单元格的..还有对应NPO ...
- HBase 数据迁移
最近两年负责 HBase,经常被问到一些问题, 本着吸引一些粉丝.普及一点HBase 知识.服务一点阅读人群的目的,就先从 HBase 日常使用写起,后续逐渐深入数据设计.集群规划.性能调优.内核源码 ...
- Koa2学习(一)环境搭建
Koa2学习(一)环境搭建 koa2脚手架 koa2服务安装 koa2-generator目录结构 什么是 Koa2 koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更 ...
- intellij IDEA破解
方法1 填入下面的license server: http://intellij.mandroid.cn/ http://idea.imsxm.com/ http://idea.iteblog.com ...
- YTU 2430: C语言习题 链表建立,插入,删除,输出
2430: C语言习题 链表建立,插入,删除,输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 576 解决: 280 题目描述 编写一个函数creatlink,用来建立一个动态链表 ...
- 《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
作者:董波 日期:2010.6.15 写在前面 在我还在上学的时候,我选择了C++,最初我用VC6作为我的IDE,我看过很多本C++的教材,有的适合我,有的不适合我,其中有一本叫<Visual ...
- 并不对劲的bzoj5340:loj2552:uoj399:p4564: [Ctsc2018]假面
题目大意 有\(n\)(\(n\leq200\))个非负整数\(m_1,m_2,...,m_n\)(\(\forall i\in[1,n],m_i\leq100\)),有\(q\)(\(q\leq2* ...