【一天一道LeetCode】#172. Factorial Trailing Zeroes
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
(二)解题
题目大意:求n的阶乘算出来的数尾部有多少个0。如5!=120,尾部有1个0,返回1。
解题思路:仔细观察阶乘公式1*2*3….*n,只有2*5=10,这样才能有0,所以一开始想到的解法是算每个数的因子里面还有2和5的个数,这两个数组成一对就代表阶乘尾部有一个0,所以求这两个因子个数的最小值即可。
下面是TLE的版本,超时了。
class Solution {
public:
int trailingZeroes(int n) {
int count2 = 0;
int count5 = 0;
for(int i = 1 ;i <= n ;i++)
{
int temp = i;
while(temp%2==0){//求因子2的个数
count2++;
temp/=2;
}
while(temp%5==0){//求因子5的个数
count5++;
temp/=5;
}
}
return min(count2,count5);//返回较小值。
}
};
超时之后,想了很久,在纸上推算了一下,发现根本不用取这两者的最小值,5的个数一定比2小,这样一来只需要判断因子5的个数就行。
如果想上述解法那样粗暴的判断每一个数中含有因子5的个数肯定是不行了。
于是想到5的因子基本上每隔5个数产生一个,n/5就能找出因子5的个数,
但是诸如25,125这种含有多个因子5的数,一次n/5肯定不对。还需要n/25才行……
这样一直推算下去,最有因子5的总个数为n/5+n/25+n/125+……
根据这个思路可以写下如下代码:
class Solution {
public:
int trailingZeroes(int n) {
int sum = 0;
while(n){
sum+=n/5;
n/=5;
}
return sum;
}
};
【一天一道LeetCode】#172. Factorial Trailing Zeroes的更多相关文章
- [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 ...
- ✡ 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 ...
- 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
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
- 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 Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
随机推荐
- Python【第五课】迭代器,生成器,数据序列化
本节内容 列表生成式,生成器,迭代器 Json & pickle 数据序列化 1.列表生成式,生成器,迭代器 1.1 列表生成式 列表生成式?不就是生成个列表的表达式,恩~~~ 差不多. 一般 ...
- sharepoint环境安装
SharePoint 2013 测试环境安装配置指南 软件版本 Windows Server 2012 标准版 SQL Server 2012 标准版 SharePoint Server 2013 企 ...
- IE6浏览器有哪些常见的bug,缺陷或者与标准不一致的地方,如何解决
IE6不支持min-height,解决办法使用css hack: .target { min-height: 100px; height: auto !important; height: 100px ...
- VMWare 学习目录
Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接.nat. ...
- mysql catalog的名字
def 算是一个一点卵用都没有的知识点 然后tmd各个版本不同 用这个语句查 SELECT * FROM information_schema.SCHEMATA where schema_name=' ...
- Java常量初始化后不会再去重新获取
Java虚拟机编译机制:更改常量部分 最近一个Java项目中需要修改一个静态常量的值,本地修改编译以后调试正常,然后把对应的entity类的class文件上传到服务器对应的目录以后系统依旧我行我素,各 ...
- 单页应用动态设置页面title
1.适用场景:所有通过router路由的单页应用. 2.示例代码:本文以vue-router为例. 在router.js中: let router = new Router({ routes: [ { ...
- async/await 的一些知识
博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...
- 中间件——canal小记
接到个小需求,将mysql的部分数据增量同步到es,但是不仅仅是使用canal而已,整体的流程是mysql>>canal>>flume>>kafka>> ...
- 2018 dnc 公司案例大全,迎接.NET Core开源新时代
2018 dnc 公司案例大全,迎接.NET Core开源新时代 dnc = .NET Core.dotnet Core dnc是微软新一代主力编程平台,开源.免费.跨平台.轻量级.高性能,支持L ...