My 4ms C++ solution by counting 5's


  • 0
    I
    class Solution {
    public:
        int trailingZeroes(int n) {
            int limit = int(log(n)/log(5));
            int count = 0;
            int i;
            for(i = 1;i<=limit;i++)
                count = count+n/pow(5,i);
            return count;
        }
    };
    

    The reason is that one 2 and one 5 together contribute a 10 (trailing zero), and that 2's are much more than 5's. Thus counting 5's is sufficient.


Log in to reply
 

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