9 ms implementation


  • 1
    Y

    int trailingZeroes(int n) {
    if(n<=0) return 0;

        int numZero=0;
        while(n>0){
            numZero+=n/5;
            n=n/5;
        }
        return numZero;
    }

  • 0
    G

    Interestingly, the following code exceeded the time limit. Guess doing big number calculation costs more time, or smaller numbers are optimized to run faster (big or small numbers are all within limit of int).

    int trailingZeroes(int n) {
    int ret = 0;
    int multi = 5;
    while(multi < n){
        ret += n/multi;
        multi *= 5;
    }
    return ret;
    

    }


  • 0
    L

    Note that multi might overflow for big n!


Log in to reply
 

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