Given a 32-bit signed integer, invert the numbers in the integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 output: -321
Example 3:
Input: 120 Output: 21
Notice:
Suppose that our environment can only store 32-bit signed integers in the range [−2^31, 2^31 − 1]. According to this assumption, if the inverted integer overflows, 0 is returned.
class Solution { public: int reverse(int x) { int t=0; while(x!=0) { if(t<=INT_MAX/10 && t>=INT_MIN/10) { t=t*10+x%10; x=x/10; } else { return 0; } } return t; } };
The idea of this program is very simple, mainly considering the boundary value, such as the case of INT_MAX, INT_MAX-1, INT_MIN, INT_MIN+1, and for the case of negative numbers, we can directly use the above processing to retain the negative sign, so No extra consideration.