leetcode python3 简单题172. Factorial Trailing Zeroes

1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第一百七十二题

(1)题目
英文:
Given an integer n, return the number of trailing zeroes in n!.

中文:
给定一个整数 n,返回 n! 结果尾数中零的数量。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/majority-element

(2)解法
记录阶乘有多少个5的因子即可,因为每两个因子就会出现一个含2的因子,而每5个因子就会出现一个含5的因子,所以只要能出现含5的因子就能找到相应的2与之配对,产生0。
(耗时:40ms,内存:13.7M)

class Solution:
    def trailingZeroes(self, n: int) -> int:
        p = 0 
        while n >= 5: 
            n = n // 5 
            p += n 
        return p

猜你喜欢

转载自blog.csdn.net/qq_37285386/article/details/105938357