题目描述:
示例:
示例 1: 输入: 3 输出: 0
解释: 3! = 6, 尾数中没有零。
示例 2:
输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.
解答代码:
class Solution:def trailingZeroes(self, n: int) -> int:sum = 1for i in range(1, n + 1):sum *= icount = 0stringSum = str(sum)print(stringSum)for i in range(len(stringSum) - 1, -1, -1):if stringSum[i] == "0":count += 1else:breakreturn count
解答思路:
笨方法,先求阶乘再算0,最后超时。
解答代码(更正):
class Solution:def trailingZeroes(self, n: int) -> int:count = 0while n >= 5:count += n // 5n = n // 5return count
解答思路:
因为0 是2 * 5 产生的,且2永远比5多,所以计算5的个数即可。
因为25是2个5,125是三个5,以此类推。
所以用while。
