int trailingZeroes(int n) {
if(n<=0) return 0;
int numZero=0;
while(n>0){
numZero+=n/5;
n=n/5;
}
return numZero;
}
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;
}