The real correct code or .. Bugs?


  • 0
    W
    public class Solution {
        public int trailingZeroes(int n) {
            int i = 5; int r = 0;
            while(n/i > 0){
                r += n/i; 
                if (Integer.MAX_VALUE / i > 0) i *= 5;
                else return r;
            }
            return r;
        }
    }
    

    my code didn't pass the test case of 1808548329,
    (my output: 452137080, 'expected output': 452137076)
    but I am still thinking my code is what is correct.
    I know some code is slightly different from mine and is accepted, as below:

    public int trailingZeroes(int n) {
            int r = 0;
            while (n > 0) {
                n /= 5;
                r += n;
            }
            return r;
        }
    

    but every time you use n /= 5, the 'real n' is no longer what it was, there's some 'loss'!
    My solution doesn't change the value of n at all... and the basic philosophy is the same..
    anyway, if you disagree, could you let me know where my code is wrong?? thanks!!!


  • 0
    J

    @waerte said in The real correct code or .. Bugs?:

    int i = 5; int r = 0;
    while(n/i > 0){
    r += n/i;
    if (Integer.MAX_VALUE / i > 0) i *= 5;
    else return r;
    }
    return r;

    At some point i will overflow.


Log in to reply
 

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