Reverse Integer
Turn an int around digit by digit, and return 0 when the reversed number no longer fits in an int.
Given a 32-bit signed integer xlong
Example 1
Output: 321
Example 2
Output: -321
Example 3
Output: 21
Example 4
Output: 0
Explanation: 9646324351 does not fit in 32 bits.
The Idea
On paper, reversing 123 is easy: read it from the right and write 321. A program holds 123 as a number, though, not as three written characters. So how does it reach the digits one at a time, starting from the right?
Dividing by 10 does it. The remainder of 123 divided by 10 is 3, the digit on the right, and Java writes that remainder as 123 % 10123 / 10
Building the answer works the other way round. Start with 0. To add a digit on the right, multiply what you have by 10 and add the digit. So 0 becomes 3, then 3 × 10 + 2 = 32, then 32 × 10 + 1 = 321. Adding a digit this way is called pushing it onto the result. Here is the whole run for Example 1:
In the top part of the picture, the column "x left" shrinks from 123 to 12 to 1. The column "last digit (x % 10)" reads 3, 2, 1, and "result so far" grows from 3 to 32 to 321. Example 3 works the same way: 120 gives the digit 0 first, and 0 × 10 + 0 is still 0, so the zero simply disappears and the answer is 21.
Example 4 raises the harder question. The reverse of 1534236469 is 9,646,324,351, but the largest value a Java int
It can check one step early. Look at the bottom part of the picture. Before pushing the final digit, 1, the result so far is 964632435. Multiplying it by 10 would give more than nine billion. Any result so far that is larger than the largest intint
Solution Approach
Three things are left to settle: how negative numbers are handled, exactly when a push would go past the int
Negative numbers
Does -123 need its own branch? In Java it does not. -123 % 10-123 / 10
When a push goes out of range
Which values of the result so far are safe to push onto? The top of the range is 2147483647, and Integer.MAX_VALUE / 10
The bottom of the range is -2147483648, one further from zero than the top. Integer.MIN_VALUE / 10
Checking before the push
Why not push first and test the answer afterwards? Because by then the value is already wrong. When an int
With a longintint
Java Solution
The method reverse
- First, set up
as 0. It holds the digits reversed so far.result - Start a loop that runs while
is not 0. Inside it, take the last digit intox withdigit , and drop that digit fromx % 10 withx .x /= 10 - Before pushing, compare
withresult . If it is bigger, or equal with a digit above 7, return 0.Integer.MAX_VALUE / 10 - Make the same check on the negative side, against
and a digit below -8, and return 0 if it fails.Integer.MIN_VALUE / 10 - Push the digit with
. Whenresult = result * 10 + digit reaches 0, the loop ends and the method returnsx .result
The main
public class ReverseInteger {
public static int reverse(int x) {
int result = 0;
while (x != 0) {
int digit = x % 10; // last digit, with the sign of x: -123 % 10 == -3
x /= 10; // drop that digit
if (result > Integer.MAX_VALUE / 10
|| (result == Integer.MAX_VALUE / 10 && digit > 7)) {
return 0; // result * 10 + digit would pass 2147483647
}
if (result < Integer.MIN_VALUE / 10
|| (result == Integer.MIN_VALUE / 10 && digit < -8)) {
return 0; // result * 10 + digit would pass -2147483648
}
result = result * 10 + digit;
}
return result;
}
public static void main(String[] args) {
int[] inputs = {123, -123, 120, 1534236469, 0, 1463847412, -2147483412,
Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int x : inputs) {
System.out.println(x + " -> " + reverse(x));
}
}
}
Running it prints:
123 -> 321
-123 -> -321
120 -> 21
1534236469 -> 0
0 -> 0
1463847412 -> 2147483641
-2147483412 -> -2143847412
2147483647 -> 0
-2147483648 -> 0
The four examples come out as 321, -321, 21 and 0. For 0 the loop never runs, so result
Look at 1463847412 -> 2147483641result
The last two lines are the ends of the range. Reversed, 2147483647 would be 7,463,847,412 and -2147483648 would be -8,463,847,412, both far outside, so both return 0.
Complexity
The method does a fixed amount of work for each digit of x
- Time: O(log |x|), because the loop removes one decimal digit of
per pass, and anx has at most 10 of them.int - Space: O(1), because the method keeps only
andresult and never turns the number into a string or an array of digits.digit