LeetCode 172 – Python 3 (Week 05 – 17)

Solution 1

class Solution:
    def trailingZeroes(self, n: int) -> int:
        
        ans = 0
        
        while n > 0:
            ans += n // 5
            n = n // 5
            
        return ans

Time complexity is O(log5). Space complexity is O(1).

Leave a comment

Design a site like this with WordPress.com
Get started