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 ...
随机推荐
- potplayer录制视频包含字幕
用potplayer录制视频,只能保存视频,外挂字幕的视频字幕无法录制进去 在字幕设置里将几个选项更改,即 字幕输出方式改为直接, 渲染方式不要选矢量即可. 如图:
- python学习之路---day16--面向对象
面向对象及成员的基本总结复习一:先用一个实际例子说明类: class 类名: class Car: #__init__ :使我们在具体构造一个对象时,使这个对象有具体的属性,像有名字啊,价格等等这样的 ...
- 测开之路七十二:性能测试工具之locust简介
locust官网:https://locust.io/ locust安装(不支持python3.7):pip install locustio 或者pycharm安装 官网给出的样例 根据官网代码 ...
- DEDECMS为文章添加NEW标志图片
找到extend.func.php添加自定义函数: function shownewimg($pubtime){$ntime = time();$tagtime = $pubtime;$day3 = ...
- Vue过渡:用Velocity实现JavaScript钩子
Velocity is an animation engine with a similar API to jQuery's $.animate(). It has no dependencies, ...
- eclipse配置,快捷键备忘
1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...
- maven 配置阿里云中央仓库
一.修改maven根目录下的conf文件夹中的setting.xml文件 <mirror> <id>alimaven</id> <name>aliyun ...
- vue2.0中router-link详解
vue2.0中router-link详解:https://blog.csdn.net/lhjuejiang/article/details/81082090 在vue2.0中,原来的v-link指令已 ...
- 10、numpy——位运算
NumPy 位运算 NumPy "bitwise_" 开头的函数是位运算函数. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 b ...
- Spring bean相关
Spring中指定Bean的作用于的方式 以下四种为例: 单例(默认,可以不用特殊表明) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) ...