My correct trailingZeroes solution doesnt get accepted, fails on 13


  • 0
    R

    i was working on the factorial trailing zeros problem. i found the answer but i somehow fail the test for 13!. i compile my code on my own machine using g++ and get the correct answer but the site tells me i am wrong.

    here is my code for reference:

    class Solution {
    public:
        int trailingZeroes(int n) {
            unsigned long ans = 1;
            while(n > 1) {
                ans *= n;
                n -= 1;
            }
            int zeros = 0;
            while(ans%10 == 0) {
                ans /= 10;
                zeros += 1;
                if(ans == 0)
                    break;
            }
            return zeros;
        }
    };

  • 0
    E

    Beware that factorial grow VERY fast.


  • 0
    B

    You have an integer overflow problem.


  • 0
    M

    Your algorithm is o(n) right off the bat in the while loop. There is o(long(n)) solution.


Log in to reply
 

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