Example 1:
Input: x = 120
Output: 21
public class ReverseInteger {
public static void main(String[] args) {
ReverseInteger solution = new ReverseInteger();
int x = 123;
System.out.println("Input: " + x);
System.out.println("Output: " + solution.reverse(x));
x = -123;
System.out.println("\nInput: " + x);
System.out.println("Output: " + solution.reverse(x));
x = 120;
System.out.println("\nInput: " + x);
System.out.println("Output: " + solution.reverse(x));
x = 0;
System.out.println("\nInput: " + x);
System.out.println("Output: " + solution.reverse(x));
}
public int reverse(int x) {
int rev = 0;
while (x != 0) {
int digit = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && digit > 7)) return 0;
if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && digit < -8)) return 0;
rev = rev * 10 + digit;
}
return rev;
}
}
- Initialize a variable
rev
to store the reversed integer. This will be the return value. - Iterate while
x
is not equal to 0: a. Get the rightmost digit ofx
using the modulus operator%
and store it in thedigit
variable. b. Remove the rightmost digit fromx
by dividing it by 10 (x /= 10
). c. Check if addingdigit
torev
would cause an overflow. If it would, return 0. - Return
rev
, which now holds the reversed integer.
Explanation of the overflow check:
- For positive numbers: If
rev
is greater thanInteger.MAX_VALUE / 10
, adding any more digits will cause an overflow. - For negative numbers: If
rev
is less thanInteger.MIN_VALUE / 10
, adding any more digits will cause an overflow.
If the input x
is 0, the loop will not run, and 0 will be returned, which is the correct result.
In the provided code, digit > 7
and digit < -8
are used as conditions to check for overflow. Let’s break down why these conditions are used and how they help prevent overflow when reversing the digits of an integer.
- For positive numbers:
- When reversing a positive number, if the reversed number (
rev
) is greater thanInteger.MAX_VALUE / 10
, then adding the next digit (digit
) will cause an overflow. For example, ifrev
is 214748364, and the next digit is 8, adding 8 would result in a number greater thanInteger.MAX_VALUE
. - Therefore, the condition
rev > Integer.MAX_VALUE / 10
is used to check if adding the next digit will cause an overflow. - Additionally, if
rev
is exactly equal toInteger.MAX_VALUE / 10
(i.e.,rev
is 214748364), and the next digit (digit
) is greater than 7, adding the next digit will also cause an overflow. This is because the maximum positive 32-bit integer is 2147483647, and adding any digit greater than 7 will exceed this limit.
- When reversing a positive number, if the reversed number (
- For negative numbers:
- When reversing a negative number, if the reversed number (
rev
) is less thanInteger.MIN_VALUE / 10
, then adding the next digit (digit
) will cause an overflow. For example, ifrev
is -214748364, and the next digit is -8, adding -8 would result in a number less thanInteger.MIN_VALUE
. - Therefore, the condition
rev < Integer.MIN_VALUE / 10
is used to check if adding the next digit will cause an overflow. - Additionally, if
rev
is exactly equal toInteger.MIN_VALUE / 10
(i.e.,rev
is -214748364), and the next digit (digit
) is less than -8, adding the next digit will also cause an overflow. This is because the minimum negative 32-bit integer is -2147483648, and adding any digit less than -8 will exceed this limit.
- When reversing a negative number, if the reversed number (
In summary, the conditions digit > 7
and digit < -8
are used to check if adding the next digit will cause an overflow in the positive and negative directions, respectively, when reversing the digits of an integer.