简体   繁体   English

反向整数leetcode:为什么仅在INT_MAX中添加7或更大的值时才会发生溢出?

[英]Reverse Integer leetcode: Why does overflow occur only if 7 or greater is added to INT_MAX?

I have the solution provided by Leetcode and the part that confuses me is the fact that adding 7 (or a lower value) to Integer.MAX_VALUE or adding -8 (or a lower value) to Integer.MIN_VALUE does not result in overflow or underflow respectively. 我有Leetcode提供的解决方案,而令我感到困惑的部分是,在Integer.MAX_VALUE中添加7(或更低的值)或在Integer.MIN_VALUE中添加-8(或更低的值)不会导致上溢或下溢分别。

My logic is that if you have Integer.MAX_VALUE, adding 1 will cause overflow. 我的逻辑是,如果您具有Integer.MAX_VALUE,则加1将导致溢出。 And if you have Integer.MIN_VALUE, subtracting 1 will cause underflow. 并且如果您有Integer.MIN_VALUE,则减1将导致下溢。 Where is my understanding of overflow and underflow wrong? 我对上溢和下溢的理解错在哪里?

class Solution {
    public int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE/10 || (rev ==     Integer.MAX_VALUE / 10 && pop > 7)) return 0;
            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }
}

Yes, normally adding 1 to Integer.MAX_VALUE will result in overflow, as will subtracting 1 from Integer.MIN_VALUE . 是的,通常向Integer.MAX_VALUE加1会导致溢出,也会从Integer.MIN_VALUE减去1。 But that is not what is happening here. 但这不是这里发生的事情。

This code is performing integer division by 10 , which truncates any decimal portion. 此代码执行的整数除以10 ,这会将任何小数部分都截断。 When dividing Integer.MAX_VALUE ( 2147483647 ) by 10, the code anticipates multiplying by 10 and adding the next digit. Integer.MAX_VALUE2147483647 )除以10时,该代码预期将乘以10并加下一位。 That quotient is 214748364 , and multiplying by 10 is 2147483640 , with the possibility of adding another 7 without overflowing. 该商为214748364 ,乘以102147483640 ,并且可以加上另外的7而不会溢出。 Similarly on the negative side, dividing Integer.MAX_VALUE ( -2147483648 ) by 10 yields -214748364 , multiplying by 10 yields -2147483640 , with the possibility of adding another -8 without overflowing. 类似地,在负方面,将Integer.MAX_VALUE-2147483648 )除以10将产生-214748364 ,再乘以10将产生-2147483640 ,并且可以加上另一个-8而不会溢出。

This code takes into account the last digit of the extremes of the range of Integer values and carefully avoids overflow. 该代码考虑了Integer值范围的极值的最后一位,并小心地避免了溢出。

Integer.MAX_VALUE is 2147483647 Integer.MAX_VALUE2147483647

That means: Integer.MAX_VALUE/10 is 214748364 这意味着: Integer.MAX_VALUE/10214748364

And you're about to do: rev = rev * 10 + pop 您将要做: rev = rev * 10 + pop

So if rev > 214748364 , then rev * 10 will cause overflow. 因此,如果rev > 214748364 ,则rev * 10将导致溢出。

Or if rev == 214748364 , then rev * 10 is 2147483640 , and rev * 10 + pop will cause overflow if pop > 7 . 或者,如果rev == 214748364 ,则rev * 102147483640 ,如果pop > 7 ,则rev * 10 rev * 10 + pop将导致溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM