172. Factorial Trailing Zeroes

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

Note: Your solution should be in logarithmic time complexity.

there should no multiply anywhere in the code, other wise there will be overflow.

consider 125, there are multiple fives in the number to end up trailing zeros. so you need to find such number recursively.

public class Solution {
    public int trailingZeroes(int n) {
        int res = 0;

        while(n >= 5){
            res += n/5;
            n /= 5;
        }
        return res;
    }
}

results matching ""

    No results matching ""