【一天一道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 ...
 
随机推荐
- hdu 4267 线段树间隔更新
			
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
 - [BZOJ]3110 K大数查询(ZJOI2013)
			
这大概是唯一一道小C重写了4次的题目. 姿势不对的树套树(Fail) → 分块(Fail) → 整体二分(Succeed) → 树套树(Succeed). 让小C写点心得静静. Description ...
 - Spring MVC页面重定向
			
以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面. 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 ...
 - PostgreSQL 中如何实现group_concat
			
之前在MySQL中使用group_concat,觉得超级好用. 今天在PostgreSQL需要用到这样的场景,就去学习了一下. 在PostgreSQL中提供了arr_agg的函数来实现聚合,不过返回的 ...
 - spring的 @Scheduled的cron表达式
			
网上太多说的多,但却没什么用的文章了 序号 说明 是否必填 允许填写的值 允许的通配符1 秒 是 0-59 , ...
 - java-反射深度剖析
			
Java反射是Java语言一个很重要的特征,简单剖析下反射的定义.原理.使用.性能及应用场景. (一)定义 程序运行时,允许改动程序结构或变量类型,这种语言称为动态语言.java不属于动态语言,但提供 ...
 - Docker学习系列(二)Docker初体验
			
一.系统要求 Docker的安装,需要在CentOS 7.0+版本,内核至少3.10,64-bit uname --r [randy@randysun ~]$ uname --r -.el7.x86_ ...
 - CentOS 7下GitLab搭建及配置
			
由于公司业务,需要上Git版本控制. * 目前市面上比较有名的Git服务提供商,国外有GitHub.BitBucket.GitLab,国内有码云,Coding. * 现有的服务商,对于免费的套餐都有一 ...
 - async/await 的一些知识
			
博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...
 - FJUT寒假作业第二周G题解快速幂
			
题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=161#P6 题意:求n个数字的乘积对c取摸.主要就是有快速幂扩展到广义幂的过程. 首先题目 ...