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 ...
随机推荐
- noi.ac #227 random
分析 我们发现实际只要计算a[i]>b[j]和a[i]<b[j]哪种多即可 代码 #include<bits/stdc++.h> using namespace std; ], ...
- php登陆ssh执行命令
<?php $connection=ssh2_connect('172.16.10.3',22);ssh2_auth_password($connection,$user,$pass);$cmd ...
- python匿名函数应用--动态计算器
匿名函数: 一般用于函数体较为简单的运算,和一些不需要函数名的传递. lambda 参数1:运算举例: func = lambda x,y:x+y :可以给匿名函数添加变量名. 匿名函数的应用: ...
- SPOJ NICEBTRE - Nice Binary Trees(树 先序遍历)
传送门 Description Binary trees can sometimes be very difficult to work with. Fortunately, there is a c ...
- (转载)Spring与SpringMVC父子容器的关系与初始化
转自 https://blog.csdn.net/dhaiuda/article/details/80026354 Spring和SpringMVC的容器具有父子关系,Spring容器为父容器,Spr ...
- #python# 使用代理和不使用代理对比
import urllib.request url='http://httpbin.org/ip' #不使用代理 response1=urllib.request.urlopen(url) #设置代理 ...
- Ecshop 商品详情页如何添加立即购买按钮
1,加到位置 <li class="add_cart_li"> <a href="javascript:addToCart1({$goods.goods ...
- python学习第十二天列表的循环,排序,统计操作方法
python列表最重要的列表的循环,任何有序列表离不开循环,列表的循环 for in range等关键词,还有列表排序,正序,倒序,还有列表每个元素的最大,最小,统计元素的个数等. 1,列表的循环 ...
- JS :Date日期格式化
Date.prototype.format = function (formatStr) { var date = this; /* 函数:填充0字符 参数:value-需要填充的字符串, lengt ...
- Django学习——开发你的第一个Django应用1
突然对Django热情似火,所以就开学习了,我是根据官方文档学习的,所以我打算把官方文档翻译一遍,全当学习,首先贴官方文档的地址:https://docs.djangoproject.com/en/1 ...