Python3解leetcode Factorial Trailing Zeroes
问题描述:
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!中,若想在结果的结尾产生0,只能是5乘以双数、或者某个乘数结尾为0,如10,但10可视为5*2,20可以视为5*4.
综上要想找n!中有几个0,其实就是寻求在1到n这n个数中有几个5.
其中25=5*5,这需要视为2个5
代码目的就变成了寻找1到n这n个数中5的个数
代码:
- def trailingZeroes(self, n: int) -> int:
- if n <= 0: return 0
- return sum( (n//(5**j)) for j in range(1, int(math.log(n, 5)) + 1))
n//(5**j) ,当j=1时,就是寻找在1到n这n个数中有几个5
n//(5**j) ,当j=2时,就是寻找在1到n这n个数中有几个25(5*5)(在上一步计算中,25会被统计,一次,但由于25是5*5,内部含有两个5,因而在第二步需要再统计一次,即一共是算为2次)
依次类推
最后将结果累计相加,就可以计算出就是寻找在1到n这n个数中有几个5了
- math.log(n, 5) 是求出以5为底,n的对数,然后向下取整,这个数就是j的最大值,因为如果j如果继续加1,那么5**j就会大于n,n//(5**j)恒为0,就没有计算意义了
Python3解leetcode Factorial Trailing Zeroes的更多相关文章
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes (阶乘后缀零)
题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...
- 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 ...
随机推荐
- Oracle命令行模式,批量执行SQL脚本
由于项目不同,使用的数据库也不一样,通常MySQL 比较方便简介,相对而言Oracle比较繁琐一点,尤其是堡垒机的连接的时候, 通过堡垒机登陆,数据库服务器,通过下面的脚本执行进入到命令行模式执行SQ ...
- vue里面的this指向
this.$http.jsonp(api).then(function(response){ console.log(response); console.log(this); this.list=r ...
- CentOS 7.0 配置防火墙
停用了 iptables. systemctl stop iptables.service 然后来启动 firewalld 吧 systemctl start firewalld.service 给我 ...
- cron 定时任两种配置方式
第一种:xml文件方式 <bean id="commonTimer" class="com.course.wx.timer.CommonTimer"> ...
- Vue过滤器:全局过滤器
Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化. 过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持). 过滤器应该被添加在 JavaScr ...
- Hadoop: 在Azure Cluster上使用MapReduce
Azure对于学生账户有260刀的免费试用,火急火燎地创建Hadoop Cluster!本例子是使用Hadoop MapReduce来统计一本电子书中各个单词的出现个数. Let's get hand ...
- 【小刘的linux学习笔记 】——01认识操作系统
1.操作系统的地位 计算机系统由硬件和软件两部分组成.通常把未配置软件的计算机称为裸机.直接使用裸机不仅不方便,而且将严重降低工作效率和机器的利用率. 操作系统(OS,Operation System ...
- Dockerfile设置时区alpine
背景: 最近在写golang相关代码.其中用到了时间操作的相关函数,如下: nowTime := time.Now() nUnixEndTime := nowTime.Unix() nHour, nM ...
- Linux笔记2-常用命令
1.简单的命令 cd / 切到根路径 cd .. 回到上一级目录 pwd 显示当前路径 touch newFile 创建文件 mkdir xx 创建目录 mv file1 ...
- javascript的继承模式
在javascript里面看到javascript的继承模式和传统的继承模式是有区别的,就想查资料看一下到底有区别,就看到了这篇文章,觉得讲得还可以,暂时先放上来,以后有别的东西再补充: http:/ ...