An intuitive solution accepted as best submission in C, well commented


  • 0
    //AC - 4ms;
    int reverse(int n)
    {
        if(n==INT_MIN || n==INT_MAX) return 0; //two extreme values are ignored;
        bool negative = n<0? true : false;
        n = negative? -n : n;
        long long a = 0;
        while(n) //reverse n;
        {
            a = 10*a + n%10;
            n /= 10;
        }
        if(a > INT_MAX) return 0; //in case of reversed big number;
        return negative? -a : a;
    }

  • 0
    H

    this sentence maybe wrong:
    n = negative? -n : n;

    for example:
    if n=INT_MIN,then -n=-INT_MIN
    but -INT_MIN is bigger than INT_MAX
    (I test this on java)


  • 0

    The first statement in this method is to avoid these two corner cases, and just return 0.


Log in to reply
 

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