LeetCode(172)Factorial Trailing Zeroes
题目
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
分析
题目描述:给定一个整数n,求对于n!末尾0的个数。
开始看到的时候并没有什么思路,只知道n!=1∗2∗3∗...∗n
那么末尾0是怎么产生的呢,必然是 质因数 2∗5而导致的结果 , 又搜索了网上一些资料:
对n!做质因数分解n!=2x∗3y∗5z∗...
显然0的个数等于min(x,z),并且min(x,z)==z
证明:
对于阶乘而言,也就是1∗2∗3∗...∗n
[n/k]代表1−n中能被k整除的个数
那么很显然
[n/2]>[n/5](左边是逢2增1,右边是逢5增1)
[n/22]>[n/52](左边是逢4增1,右边是逢25增1)
……
[n/2p]>[n/5p](左边是逢2p增1,右边是逢5p增1)
随着幂次p的上升,出现2p的概率会远大于出现5p的概率。
因此左边的加和一定大于右边的加和,也就是n!质因数分解中,2的次幂一定大于5的次幂
此时,便很明了了,结果可以表示为:
n!后缀0的个数 = n!质因子中5的个数 = floor(n/5)+floor(n/25)+floor(n/125)+....
AC代码
class Solution {
public:
int trailingZeroes(int n) {
int count = 0;
while (n)
{
count += n / 5;
n /= 5;
}
return count;
}
};
LeetCode(172)Factorial Trailing Zeroes的更多相关文章
- LeetCode(73)Set Matrix Zeroes
题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- (转)Linux下java进程CPU占用率高-分析方法
Linux下java进程CPU占用率高-分析方法 原文:http://itindex.net/detail/47420-linux-java-%E8%BF%9B%E7%A8%8B?utm_source ...
- P3290 寻找第K大数
描述 寻找第K大数 N个小朋友在一起做游戏.每个小朋友在自己的硬纸板上写一个数,然后同时举起来.接着,小y老师提一个问题,看哪个小朋友先抢答出来.问题是:在这N个数中,第K大的是哪个数?请你编程完成. ...
- UI2_异步下载
// AppDelegate.m // UI2_异步下载 // // Created by zhangxueming on 15/7/17. // Copyright (c) 2015年 zhangx ...
- easyui常用控件及参数说明
CSS类定义: div easyui-window window窗口样式 属性如下: 1) modal:是否生成模态窗口.tru ...
- SharePoint运行状况分析器有关磁盘空间不足的警告
对于负责管理SharePoint内部部署安装的SharePoint管理员,SharePoint Health Analyzer是一款出色的工具.此功能不仅有助于解决服务器故障和服务失败的问题,还提供了 ...
- LoadRunner创建脚本和场景流程
1)脚本创建流程创建脚本->选择协议-设置录制选项-录制脚本-停止录制-优化脚本(去掉无用内容)-强化脚本(注释.代码结构调整.参数化.检查点.事物.关联)-调试脚本(观察日志) 2)场景设置的 ...
- 单列表变量与字符串拆分的对照(SqlServer)
最近遇到一个问题,在SQLServer中,需要根据用户传入的一系列ID值更新对应的记录.有两种方法,一种是将这些ID值使用逗号分隔,拼接成字符串传入,一种是以表变量的方式传入.最开始,我想当然的认为传 ...
- codeforce Gym 100500H ICPC Quest (简单dp)
题意:给一个nXm的矩阵,上面有一些数字,从左上角出发,每次只能往右或者往下,把沿途的数字加起来,求到达右下角的最大值是多少. 题解:简单的一个dp,设f[i][j]为到达i行j列的最大值,f[i][ ...
- coredata 删除与更新
http://blog.csdn.net/rhljiayou/article/details/18037729 //删除 -(void)deleteData { NSManagedObjectCont ...
- spring maven 包
<spring-framework.version>.RELEASE</spring-framework.version> <dependency> <gro ...