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 ...
随机推荐
- echarts Hello world 入门
<!DOCTYPE html> <html> <head> <title></title> <script type="te ...
- HTTP1.1中CHUNKED编码解析(转载)
HTTP1.1中CHUNKED编码解析 一般HTTP通信时,会使用Content-Length头信息性来通知用户代理(通常意义上是浏览器)服务器发送的文档内容长度,该头信息定义于HTTP1.0协议RF ...
- Spring Cloud--Feign服务调用组件的使用实例
引入依赖: 启动类上添加@EnableFeignClients注解: 写调用接口: 直接@Autowired注入服务调用接口: 底层使用了动态代理,对接口进行了实现. 并且封装了RestTemplat ...
- 公司项目git开发流程规范
手动修改冲突之后,git add . git commit ,git push
- 监听textarea数值变化
监听textarea数值变化 $('#id').bind('input propertychange', function(){ //TODO });
- 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式
1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...
- Android用RecyclerView实现的二维Excel效果组件
excelPanel 二维RecyclerView.不仅可以加载历史数据,而且可以加载未来的数据. 包括在您的项目中 excelPanel 二维RecyclerView.不仅可以加载历史数据,而且 ...
- Easyui validatebox后台服务端验证
Easyui validatebox的验证提示十分好用,可是在实际项目的运用中,经常会遇到需要服务器验证后并返回验证结果信息,比如验证用户名.手机号.邮箱是否已存在.于是就想着怎么拓展Easyui的验 ...
- Kail安装后的配置
安装完Kail系统后进行简单的几项配置可以让使用更方便 VMware安装Kail系统这里就不介绍了,大家可以参考这个博客:http://www.cnblogs.com/xuanhun/p/568831 ...
- C基础的练习集及测试答案(16-30)
16.(课堂)输入一个年份(正整数),判断这年是否是闰年.闰年判断标准:年份能被4整除:如若遇到100的倍数,则需判断年份能否被400整除.(逢4一闰,逢百不闰,逢400又闰) #if 0 .(课堂) ...