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,就没有计算意义了

原文地址:https://www.cnblogs.com/xiaohua92/p/11109787.html

时间: 2024-08-29 19:50:48

Python3解leetcode Factorial Trailing Zeroes的相关文章

关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0的个数(如5!=120,则后缀中0的个数为1). 解题思路: 1 int trailingZeroes(int n) { 2 return (n/5>0)?trailingZeroes(n/5)+n/5:0; 3 } 首先这是LeetCode中时间复杂度为O(logn)的解法. 未完待续.

[LeetCode] Factorial Trailing Zeroes 阶乘末尾0

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. Hide Tags Math 这题应该是2014年年底修改该过测试样本,之前的通过

Leetcode: Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解析: 只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种

[LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中10的个数,

LeetCode——Factorial Trailing Zeroes

题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解答: class Solution { public: int trailingZeroes(int n) { int i = 0; while (n >= 5) { i += n / 5; n /= 5; } return i; } }

LeetCode—Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 题目是非常简单的,计算一个数字递归相乘后末尾0的个数 在相乘出现2*5才有可能出现0,2一般是足够的,主要是5的个数,因为是阶乘,比如所以只要数字大于15,那么必定经过15,10,5那么肯定包含3个以上的5,比如25,那么肯定包含25,20,15,

LeetCode Factorial Trailing Zeroes (阶乘后缀零)

题意:如标题 思路:其他文章已经写过,参考其他. 1 class Solution { 2 public: 3 int trailingZeroes(int n) { 4 return n/5<5? n/5: n/5+trailingZeroes(n/5); 5 } 6 }; AC代码

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*