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 ...
随机推荐
- 通过sqlserver sa密码修改windows操作系统密码
如果你不记得windows管理员的密码了,但知道sqlserver sa用户的密码,可以通过以下方式修改: 进入SQL之后执行以下语句: -- 允许配置高级选项 EXEC sp_configure ...
- mysql非常全的和完整的总结
(1)数据类型 类型 备注 tinyint/smallint/mediumint/int/bigint 1B/2B/3B/4B/8B float/double 单精度/双精度浮点型 decimal 不 ...
- PHP正则表达式 - 附录(常用正则表达式)
常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...
- 关于 SQL Server Reporting Services 匿名登录的解决方案
每次访问报表都需要windows验证,这样的报表给客户确实很说不过去. SSRS 可以匿名登录的设定步骤: 环境: 开发工具:SQL Server Business Intelligence Deve ...
- [转] java实现https请求
package com.lichmama.test.util; import java.io.ByteArrayOutputStream; import java.io.IOException; im ...
- 问题:java.sql.SQLException: No value specified for parameter 1
解决方案:没有指定参数 String user = req.getParameter("user"); String pwd = req.getParameter("pw ...
- 我们为什么要看《超实用的HTML代码段》
不知道自己HTML水平如何,不知道HTML5如何进化?看这张图 如果一半以上的你都不会,必须看这本书,阿里一线工程师用代码和功能页面来告诉你每一个技术点. 都会一点,但不知道如何检验自己,看看本书提供 ...
- [Git]常用的Git命令行
Commit的用法 git init [+项目名] git add . (注意这里在add后面的空格和点是不能省略的) git status git commit -m “message”(这里的me ...
- 使用nginx搭建一个简单的负载均衡
在windows系统上使用IIS模拟出两个不同服务器的站点: 然后再NGINX使用轮询机制配置两个服务器以及虚拟服务器的端口: 需要注意的是,配置虚拟代理域名的话需要找到windowsC盘下的host ...
- 数据库要素 ER
数据库的要素即为ER: 即为表和关系. 再往下即为字段.记录. 往上即为数据操作.管理: 包含多表操作: 在往上为事务. 再往上为大数据.高并发.