Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).
Example
Given x = 123, return 321
Given x = -123, return -321
We need to deal with overflow very carefully.
One way is to use long type to explicitely check overflow by comparing with Integer.MAX_VALUE and Integer.MIN_VALUE.
Time: O(logn)
Space: O(1)
public class Solution {
private static final int BASE = 10;
public int reverseInteger(int n) {
long result = 0;
while (n != 0) {
result = result * BASE + n % BASE;
n /= BASE;
}
return (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) ?
0 : (int)result;
}
}
Another way to do that is to check if the result before adding exceeds Integer.MAX_VALUE / BASE, where BASE = 10.
public class Solution {
private static final int BASE = 10;
private static final int THRESHOLD = Integer.MAX_VALUE / BASE;
public int reverseInteger(int n) {
int result = 0;
while (n != 0) {
if (Math.abs(result) > THRESHOLD) {
result = 0;
break;
}
result = result * 10 + n % BASE;
n /= BASE;
}
return result;
}
}