My c++ solution, handling overflow case


  • 1
    W
    class Solution {
    public:
        int reverse(int x) {
            short flag = x>0? 1:-1;
            int X = abs(x);
            int res = 0;
            while(X){
                if((flag && (INT_MAX-X%10)/10 < res) || (!flag &&  INT_MIN+res*10+X%10 > 0)){
                    return 0;
                }
                res = res*10 + X%10;
                X /= 10;
            }
            return flag * res;
        }
    };

  • 0
    K

    In the while loop, I don't know exactly what's the flag to do?

    First, I use the following to get AC.

    while (X) {
    if( (INT_MAX-X%10)/10 < res)){
    return 0;
    }
    res = res*10 + X%10;
    X /= 10;
    }

    I know the job of the if condition, " res multi 10 plus X%10 is bigger than INT_MAX"——overflow.

    QA:
    What's (INT_MIN-X%10)/10 < res) mean. You get the absolute value of x as X. but in the while loop, you cosider the sign of the integer, may be something complicated happened?

    Can you explain what is purpose using (INT_MIN-X%10)/10 < res)?

    Thanks.


  • 0
    W

    Thanks for your comment!

    First,the flag is the sign of x.

    And getting the asolute value of x as X, is to make sure that X is always postive in the while loop, for the easier calculation.

    Actually, INT_MAX is 2147483647, and INT_MIN is -2147483648

    it means that abs(INT_MIN) = INT_MAX + 1, their numbers are different

    so I think it can't use the same way to handle both postive case and negative case

    therefore, when the flag is -1, which means the negative case, I use (INT_MIN-X%10)/10 < res) to handle the overflow.


  • 0
    K

    Thanks for your explaination.

    As the code you show, the value of res caculated can not overflow and is positive.

    Given X is positive, INT_MIN - X%10 is overflowed, this is is bigger positive integer.

    Can you explain why the condition (INT_MIN - X%10)/10 < res satisfied when the input is negitive, the revsed value is overflowed.

    May be something like -(INT_MIN + X%10)/10 =< res, I can understand this.

    res10 +X%10 >= -INT_MIN is the overlfowed condition, when input is negative.
    This can reduce to
    res
    10 >= -INT_MIN-X%10
    res >= (-INT_MIN-X%10) / 10


  • 0
    W

    oh, I know the problem you talk about!

    I just don't remember how I think at that moment.

    But I think you are right, something wrong with the code there, and I don't know why I get AC with this problem.

    Maybe this way of the negative overflow is more reasonable:

    -(res*10 + X%10) < INT_MIN

    and then INT_MIN+res*10+X%10 > 0

    I will edit my code for the new way

    Thanks for your suggestion!


  • 0
    C

    Hi, first thank you for your code
    My question is about the handling overflow. Why you use (INT_MAX-X%10)/10 < res rather than (INT_MAX - X%10-10*res) < 0? I tried to use the latter one, but it will cause some errors
    Appreciate your help!


  • 0
    K

    If you use the following condition judged the overflow:
    (INTMAX - X%10-10*res) < 0,

    INTMAX - X%10 < 10*res

    When it acctually overflows, 10res is negative, INTMAX - X%10 is positive, the condtion fails, it can not contains the overflow situation. The sign of (INTMAX - X%10-10res) may be positive(can do some experiment), the condition fails, can not contains the overflow.

    But use (INTMAX-X%10)/10 < res, it is coducted from INTMAX < 10*res + X%10. transfrom to (INTMAX-X%10)/10 < res, make the both side of the less equation are all positive, the same range. It can satify the purpose. And the value of res belongs to the corrent range.

    These are my understand.


Log in to reply
 

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