문제 링크: leetcode.com/problems/factorial-trailing-zeroes/
Factorial Trailing Zeroes - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
접근 방식:

팩토리얼은?
n! = n * (n-1) * (n-2) * .... (n-(n-1)) 남은 정수가 1이 될 때까지 곱합니다.
ex) 5! = 1 * 2 * 3 * 4 * 5
문제에서는 0의 개수를 묻고 있습니다. 2 * 5 = 10을 활용하면 됩니다. 이는 결국 5의 개수가 0의 개수인 것을 의미합니다.
코드:
class Solution:
def trailingZeroes(self, n: int) -> int:
ans = 0
if n < 5:
return 0
if n > 4:
i = 5
while i <= n:
ans = ans + n // i
i = i * 5
return ans
'Algorithnm > LeetCode' 카테고리의 다른 글
[LeetCode]54번 : Spiral Matrix (0) | 2021.02.24 |
---|---|
[LeetCode] 125번 : Valid Palindrome (0) | 2020.12.13 |
최근댓글