python-algorithm

leetcode 172. Factorial Trailing Zeroes

무적김두칠 2023. 6. 26. 14:14

https://leetcode.com/problems/factorial-trailing-zeroes/description/?envType=study-plan-v2&envId=top-interview-150 

 

Factorial Trailing Zeroes - LeetCode

Can you solve this real interview question? Factorial Trailing Zeroes - Given an integer n, return the number of trailing zeroes in n!. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.   Example 1: Input: n = 3 Output: 0 Explanation: 3! = 6, no tra

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from math import factorial
 
 
class Solution:
    def trailingZeroes(self, n: int-> int:
        answer = 0
        if n == 0:
            return 0
        else:
            factorial_n = factorial(n)
            while factorial_n%10 == 0:
                factorial_n//=10
                answer += 1
        
        return answer
cs
반응형