LeetCode Reverse Integer
Problem Description: https://leetcode.com/problems/reverse-integer/description/ Accepted Solution /* I have learned some crucial things during solving the problem. For example, if you want to check integer overflows for several operations, you have to manage the following conditions: int a = <something>; int x = <something>; if (x < 0 && a > INT_MAX + x) // a - x would overflow // `a - x` would overflow if (x > 0 && a < INT_MIN + x) // a - x would underflow if (a == -1 && x == INT_MIN) // a * x can overflow if (x == -1 && a == INT_MIN) // a * x (or a / x) can overflow if (x != 0 && a > INT_MAX / x) // a * x would overflow if (x != 0 && a < INT_MIN / x) // a * x would underflow Hence, check the appropriate conditions in perfect positions at your code. Also, keep your attention when you are dealing with a negative number. Moreover, a negative number is unsuitable for direct...
Comments
Post a Comment