[LeetCode][Python3] 172. Factorial Trailing Zeroes
                2018. 11. 3. 22:18 |
                
                    프로그래밍/LeetCode
                
            
            
            
        Problem :
https://leetcode.com/problems/factorial-trailing-zeroes/
My Solution :
class Solution:
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        ans = 0
        while n:
            n //= 5
            ans += n
        return ans
Comment :
10은 2와 5의 곱으로 만들어낼 수 있으며, 2는 매 짝수마다 나오기때문에 5보다 무조건 많다. 따라서 5의 개수만 세어주면 된다. (5, 25, 125, 625, ... 을 세어주는 것이다)
위 방법은 반복문을 이용한 것이고, 아래 방법은 Recursion을 이용한 것이다.
My Solution 2:
class Solution:
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n:
            return n//5 + self.trailingZeroes(n//5)
        return 0
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 202. Happy Number (0) | 2018.11.04 | 
|---|---|
| [LeetCode][Python3] 191. Number of 1 Bits (0) | 2018.11.04 | 
| [LeetCode][Python3] 190. Reverse Bits (0) | 2018.11.04 | 
| [LeetCode][Python3] 189. Rotate Array (0) | 2018.11.03 | 
| [LeetCode][Python3] 171. Excel Sheet Column Number (0) | 2018.11.03 | 
| [LeetCode][Python3] 122. Best Time to Buy and Sell Stock II (0) | 2018.11.03 | 
| [LeetCode][Python3] 88. Merge Sorted Array (0) | 2018.11.03 | 
| [LeetCode][Python3] 118. Pascal's Triangle (0) | 2018.10.17 | 
최근에 달린 댓글 최근에 달린 댓글