meaning of the title
Flip an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
analyze
Given an integer, flip the integer and
take the remainder from the last digit.
Finally, it is necessary to judge whether it is out of bounds.
code
class Solution {
public int reverse(int x) {
long result = 0;
//Process each bit in a loop, take out each bit from the back to the front, and then loop once multiplied by 10 to move forward
while (x != 0) {
result = result * 10 + x % 10;
x = x/10;
}
//Finally determine whether it is out of bounds
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) return 0;
return (int)result;
}
}