Accepted python submission with log5(n) time complexity


  • 4
    L

    5 multiply an even number will contribute one tailing zero. The factorial list never short of even number. Basically, how many '5' within the list determines the number of tailing zero. The code bellow count how many 5 in the list from 1 to n.

       class Solution:
        # @return an integer
        def trailingZeroes(self, n):
            res=0
            while n>0:
                n=int(n/5) 
                res+=n 
            return res

  • 0
    O

    Thanks for sharing.


Log in to reply
 

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