C++ 3 lines implementation


  • 14
    W

    class Solution {

    public:

    int trailingZeroes(int n) {
    
        int sum=0;
    
        for (int i = n/5; i>0; i/=5) sum += i;
    
        return sum;
    }
    

    };


  • 0
    R

    why my solution gives wrong answer;
    Solution:
    int trailingzeros(int n)
    {
    int cnt=0;
    for(int i=5;(n/i)>=1;i*=5)
    cnt+=(n/i);
    return cnt;
    }

    This solution gives output:
    Input: 1808548329
    Output: 452137080
    Expected: 452137076

    why so.. please explain...
    thanks.. :)


  • 0
    O

    Maybe the problem is : (n/i)>=1
    I thought it should be: (n/i) >= 5


  • 0
    L

    because the int overflow,in the last time i=i*5,the result should be larger than the up-limit int type and then becomes a small number,so your amount of the zeros will be more than the ans


Log in to reply
 

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