Explanation


  • 0
    T

    The trailing zeros are products of 2 and 5. We need to find the minimum number of integers whose factor have 2 and integers with factor 5. Because every 2 number has an odd number but every 5 numbers have one number with factor 5, we only care about the number of factors 5.
    One integer perhaps has one or more factors 5 and we can calculate them by using n/pow(5,k). If n can be divided by pow(5,k) then n has k 5s.
    So we calculate n/5+n/25+n/125+...
    The value v who can be divided by v/25 can also be divided by v/5. But we don't need to care about it because v has two 5s and one can be added in n/5 and one in n/25.

    public class Solution {
        int zeros = 0;
        public int trailingZeroes(int n) {
            if (n < 5){
                return 0;
            }
            else{
                return n/5 + trailingZeroes(n/5);
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.