【一天一道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 ...
随机推荐
- ●BZOJ 2006 NOI 2010 超级钢琴
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2006 题解: RMQ + 优先队列 (+ 前缀) 记得在一两个月前,一次考试考了这个题目的简 ...
- [51nod1238]最小公倍数之和V3
来自FallDream的博客,未经允许,请勿转载,谢谢. ----------------------------------------------------------------------- ...
- python 用codecs实现数据的读取
import numpy as np import codecs f=codecs.open('testsklearn.txt','r','utf-8').readlines() print(f) d ...
- C语言程序设计第一次作业(2017.10.10完成)
一:程序框图以及正确运行结果: (1)给出圆半径,得出圆面积: ①程序框图如下: ②测试图如下: 经过测试 ,输入半径2能得出正确结果.多次测试,输入不同值,均得出正确结果,证明稳定性. ③实验分析: ...
- SecureFX连接Linux后文件夹中文乱码问题解决(转)
在使用SecureFX 连接Linux 时,发现文件夹显示乱码,一直尝试各种配置,现将方法整理一下!供大家参考! 首先在选项中设置字符编码为UTF-8 然后在全局选项中找到Securefx的配置文件 ...
- openlayers3设置zoom不变
设置maxZoom和minZoom一致,并去掉resolutions
- 转:Kafka 客户端TimeoutException问题之坑
原文出自:http://www.jianshu.com/p/2db7abddb9e6 各种TimeoutException问题 会抛出org.apache.kafka.common.errors.Ti ...
- 使用CSS让多出来的字变为省略号
<style> .text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellips ...
- Python3 基础语法
编码 默认情况下,Python 3源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 标 ...
- Bootstrap3 表格-鼠标悬停
通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应. <table class="table table-hover" ...