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 ...
随机推荐
- HTML——制作一个图片列表
总结: 1)html有很多默认样式,然而实际应用中并不需要,因此要在制作样式之前清除掉默认样式. 2)注意清除margin-top塌陷 3)使用float:left后要使用clear:both清除其影 ...
- Maven--setting详解
settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它呢? ...
- Dubbo 使用rest协议发布http服务
演示用GitHub地址:https://github.com/suyin58/dubbo-rest-example 1 Dubbo_rest介绍 Dubbo自2.6.0版本后,合并了dub ...
- 一个例子说明Jsp三大重要内置对象的生命周期
此处Jsp的三大内置对象指:request,session以及application.他们共有的方法:setAttribute,getAttribute,方法名和方法作用都是相同的,但是作用范围不一样 ...
- ABAP扫雷游戏
. INCLUDE <icon>. CONSTANTS: " >> board cell values blank_hidden ', blank_marked TY ...
- ios 利用kvc 监听可变数组变化
KVO键值监听: Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观 ...
- 《spss统计分析与行业应用案例详解》:实例九 单一样本t检验
单一样本t检验的功能与意义 spss的单一样本t检验过程是瑕设检验中最基本也是最常用的方法之一,跟所有的假没检验一样,其依剧的基木原理也是统计学中的‘小概率反证法”原理.通过单一样本t检验.我们可以实 ...
- python中的getcwd
Help on built-in function getcwd in module posix: getcwd(...) getcwd() -> path Return a ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.web.socket.server.standard.ServerEndpointExporter
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/web/socket/ ...
- 三维GIS-室内寻径功能实现
期末,要交一个大作业,正巧之前跑国图借书的时候,晕头转向的,国图内居然没有导航!!!借这个机会做一个室内导航的demo,只是半成品,还需要加入室内定位,匹配一下坐标才能在实际中使用. demo:利用蜂 ...