Python solution with detailed explanation


  • 0
    G

    Solution

    Factorial Trailing Zeroes https://leetcode.com/problems/factorial-trailing-zeroes/?tab=Description

    Algorithm

    • 2 * 5 gives a trailing zero
    • We have plenty of 2
    • Just need to find 5
    • 5,10,15,20,25,...
    • But 25 gives 2 5s, 75! will have 25, 50, 75, three more
    • This will give us the algorithm
    class Solution(object):
        def trailingZeroes(self, n):
            """
            :type n: int
            :rtype: int
            """
            count = 0
            start = 5
            while start <= n:
                count = count + n//start
                start = start*5
            return count
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.