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 ...
随机推荐
- Step2 - How to: Implement a Windows Communication Foundation Service Contract
This is the second of six tasks required to create a basic Windows Communication Foundation (WCF) se ...
- hypermesh生成MNF柔性体
软件HyperworksX 2019: 该版本默认的是optistruct模板 1.导入几何→分网→材料定义(注意单位,建议统一mks单位制)→定义单元属性→创建刚性单元: 2.创建load coll ...
- Ubuntu 18.04 截图工具-flameshot(安装及使用)
安装flameshot:https://github.com/lupoDharkael/flameshot 安装命令: sudo apt-get install flameshot 设置>设备& ...
- PostgreSQL 在视频、图片去重,图像搜索业务中的应用
摘要: PostgreSQL 在视频.图片去重,图像搜索业务中的应用作者digoal日期2016-11-26标签PostgreSQL , Haar wavelet , 图像搜索 , 图片去重 , 视频 ...
- SqlServer 2012 AlwaysOn
第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnblogs.com/lyhabc/p/4682028.html第三篇htt ...
- Jenkins持续集成_02_添加python项目&设置定时任务
前言 自动化测试脚本编写后,最终目的都是持续集.持续集成可以实现一天多次部署运行自动化脚本,对功能进行不断监控测试.由于小编使用python编写的自动化脚本,这里仅讲解下如何在Jenkins中添加py ...
- hdu6699Block Breaker
Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...
- Recurrent Neural Network(3):LSTM Basics and 《Inside Out》
下图是Naive RNN的Recurrent Unit示意图,可以看到,在每个时间点t,Recurrent Unit会输出一个隐藏状态ht,对ht加工提取后将产生t时刻的输出yt.而在下一个时间节点t ...
- CSAPP:局部性原理
一个编写良好的计算机程序常常具有良好的局部性(locality).局部性通常有两种不同的形式:时间局部性(temporal locality)和空间局部性(spatial locality).在一个具 ...
- JS中设置input的type="radio"默认选中
html: <input id="Radio1" type="radio" value="男" name="st_Sex&q ...