My 6 line Python Solution


  • 0

    Understand this as counting number of times 5 appears, first count single 5, then 55, then 555, ..., for instance, if single 5 is already counted, 55 becomes single 5, ....., to count how many 5 will appear, we just need the value of n/i, and so on.
    class Solution(object):
          def trailingZeroes(self, n):
              """
              :type n: int
              :rtype: int
             """
             i = 5
             sum = 0
             while n >= i:
                  sum += n / i
                  i *= 5
             return sum


Log in to reply
 

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