# 阶乘后的零
"""
给定一个整数n ,返回n!结果中尾随零的数量。
提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
"""
import sys
class Solution:
def trailingZeroes(self, n: int) -> int:
# 直接注意字符最大转换长度即可,python3.10的功能
sys.set_int_max_str_digits(400000)
res = 1
for i in range(1, n + 1):
res *= i
count = 0
flag = True
for i in str(res)[::-1]:
if i == "0" and flag:
count += 1
else:
flag = False
return count
LeetCode 172.阶乘后的零 Python题解
最新推荐文章于 2024-06-18 00:22:55 发布