N

(Leet Code c++)Factorial Trailing Zeroes 본문

Leet Code 알고리즘

(Leet Code c++)Factorial Trailing Zeroes

naeunchan 2021. 7. 21. 11:41
728x90
반응형

172. Factorial Trailing Zeroes

 

Given an integer n, return the number of trailing zeroes in n!.

Follow up: Could you write a solution that works in logarithmic time complexity?

 

Example 1:

Input: n = 3 Output: 0 Explanation: 3! = 6, no trailing zero.

Example 2:

Input: n = 5 Output: 1 Explanation: 5! = 120, one trailing zero.

Example 3:

Input: n = 0 Output: 0

 

Constraints:

  • 0 <= n <= 10^4
class Solution {
public:
    int trailingZeroes(int n) {
        int count = 0;
        int x = 5;
        
        while(x<=n){
            count+= n/x;
            x = x*5;
        }
        
        return count;
    }
};
728x90
반응형

'Leet Code 알고리즘' 카테고리의 다른 글

(Leet Code c++)Number of 1 Bits  (0) 2021.07.21
(Leet Code c++)Reverse Bits  (0) 2021.07.21
(Leet Code c++)Excel Sheet Column Number  (0) 2021.07.21
(Leet Code c++)String to Integer(atoi)  (0) 2021.07.20
(Leet Code c++)Majority Element  (0) 2021.07.20