阶乘尾随零

时间:2018-10-20 00:42:42

标签: python-3.x

给出整数n,返回n!中尾随零的数目。

示例1:

输入:3 输出:0 说明:3! = 6,无尾随零。

这是我的代码:

class Solution(object):
    def trailingZeroes(self, n):
    """
    :type n: int
    :rtype: int
    """
        def factor(n):
            if n == 0:
                return 1
            else:
                return n * factor(n - 1)

        num = factor(n)

        def helper(s, a):
            if s % 10 == 0:
                helper(s // 10, a + 1)
            else:
                return a

        return helper(num, 0)

如果s尾部为零,为什么它返回Null?

1 个答案:

答案 0 :(得分:2)

您忘了returnhelper()的呼叫helper()的结果:

    def helper(s, a):
        if s % 10 == 0:
            helper(s // 10, a + 1)
          # ^^^ There should be a "return" here in front of "helper"
        else:
            return a
相关问题