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 ...
随机推荐
- 凉经-Mozilla Firefox Ltd
北京谋智火狐信息技术有限公司(北京市东城区建国门华润大厦17层)过去面试的时候感觉电梯好神奇啊!一边的电梯是直达18层以上的,我按了18层准备到了再往下走一层,一个老司机和我说要做另一边的1-17层的 ...
- Linux运维知识之linux shell date 用当天时间做备份文件名
本文主要向大家介绍了Linux运维知识之linux shell date 用当天时间做备份文件名,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. #!/bin/bash #dat ...
- time in china
https://www.timeanddate.com/worldclock/china
- GARENA笔试sql20190926
create database garena; use garena; create table players( account_id int, name varchar(20), country ...
- MySQL 查询语句--------------进阶7:子查询
#进阶7:子查询 /* 含义: 出现在其他语句中的select语句,称为子查询或者内查询 外部的查询语句,称为主查询或外查询 分类: 按照子查询出现的位置: select后面:只支持标量子查询 fro ...
- servlet--response、request
请求响应流程图 response 1 response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServle ...
- ubantu 安装软件
一.解压后bin文件夹里有setup.py 进入到setup.py的目录,执行命令: sudo python3 setup.py install 二.以.whl结尾的文件 直接运行命令: sudo p ...
- python包的补充
1.包A和包B下有同名模块也不会冲突,如A.a与B.a来自俩个命名空间 2.常见目录结构 import os 2 os.makedirs('glance/api') 3 os.makedirs('gl ...
- each of which 用法
each of which 在以下為 同位語,非關代. 1. An urn contains two balls, each of which is known to be either white ...
- python学习第三十二天函数的闭包
python函数中嵌套另外一个函数,另外一个函数形成一个封闭的环境,里面的那个函数叫做函数的闭包,函数的闭包好处可以保护函数里面的变量,下面讲述函数闭包的实例和用法 1,函数闭包的实例 a='cat' ...