LeetCode-172 Factorial Trailing Zeroes

Title Description

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

 

Subject to the effect

Given a positive integer n, computing n! There are several suffixes zero.

 

Examples

E1

Input: 3
Output: 0

E2

Input: 5
Output: 1

 

Problem-solving ideas

Because n! Each occurrence a 5 will produce a zero (2 * 5 * 12 = 180 = 10,15, ...), while each occurrence a 5 * 5 = 25 will occur a plurality of zero (note that this extra zero zero zero than 5 appeared previously calculated), so that each 5 * 5 * 125 = 5 also generates a plurality of zero.

 

Complexity Analysis

Time complexity: O (log (n))

Space complexity: O (1)

 

Code

class Solution {
 public :
     int trailingZeroes ( int n-) {
         int ANS = 0 ;
         // calculate save 5,25,125, ... generated zeros 
        for ( Long  Long I = . 5 ; n-/ I> 0 ; * = I . 5 ) { 
            ANS + = (n-/ I); 
        } 
        
        return ANS; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/heyn1/p/10955015.html