【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目描述
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
题目大意
递归
分析一下,就是看n!有多少个5组成。
计算包含的2和5组成的pair的个数就可以了。
因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。
观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以。
但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5),
所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, …, n/5/5/5,/5直到商为0。
最后的结果就是
return n/5 + n/25 + n/125 + n/625 + n/3125+...;
代码如下:
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
i = 5
while n >= i:
res += n / i
i *= 5
return res
循环
这个题一看就不能暴力求解。
算法中这个count += n / i;的意思是直接看有多少个5.
比如n=26;那么,26/5=5;26/25=1;所以结果是5+1=6个。
另外注意,一定用long。
public class Solution {
public int trailingZeroes(int n) {
if(n<=0) return 0;
int count = 0;
for (long i = 5; n / i >= 1; i *= 5) {
count += n / i;
}
return count;
}
}
AC:1ms
python解法如下:
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
i = 5
while n >= i:
res += n / i
i *= 5
return res
日期
2016 年 5 月 8日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)的更多相关文章
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 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 ...
- 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 ...
- [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 ...
- 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告
原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- 蛋白质组DIA深度学习之谱图预测
目录 1. 简介 2. 近几年发表的主要工具 1.DeepRT 2.Prosit 3. DIANN 4.DeepDIA 1. 简介 基于串联质谱的蛋白质组学大部分是依赖于数据库(database se ...
- Mac下source tree 下的安装
安装时出现了以下错误,解决方法 git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=source ...
- TD课程通最终版本体验
功能上,新版本增加了学校教室的上课情况,有无课程可以清楚查询,如下图: 在添加课程的设置上有改进,相比于之前编辑课程后不能保存,新版本在可保存的基础上又增加了登陆教务系统的功能,学生使用更加方便快捷, ...
- 学习java的第十五天
一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...
- SQLITE_BTREE_H
sqlite3.c, 237436行 = 全部源文件,找东西比多文件查找方便多了:-),字符串查找一点都不慢. 不要太害怕,SQLite说它的代码里有非常多是用来做数据完整性检查和测试的.但愿B树,虚 ...
- 打破砂锅问到底!HTTP和HTTPS详解
HTTP 引自维基百科HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...
- 循环队列/顺序队列(C++)
队列(queue)是一种限定存取位置的线性变.他允许在表的一端插入,在另一端删除.这个和计算机调度策略中的先来先服务FCFS(First Come/First Served)是一样的.队列中可以插入的 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- Docker学习(四)——Docker容器连接
Docker容器连接 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或-p参数来指定端口映射. 下面我们来实现通过端口连接到一个docker容器. 1.网络端口映射 ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...