topic:
Gives a 32-bit signed integer, you need this integer number on each inverted.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
note:
Suppose we have an environment can only store a 32-bit signed integer, then the value range of [-231 231--1]. Please According to this hypothesis, if integer overflow after reverse it returns 0.
Code:
class Solution { public int reverse(int x) { long result = 0; while(x != 0){ int pop = x % 10; x /= 10; result = pop + result * 10; } if(result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) return 0; else return (int)result; } }
Experience:
1, this question would be in the pit overflow judgment.
In fact, beginning to know overflow judgment, but modified many times have found to determine the failure, the output is always wrong. I do not know how to start and had looked at the solution to a problem, a rude awakening.
Because I had the result set int type, so the circle of the result has been not overflow, then the judge behind the overflow is invalid.