Problem Description
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Examples
Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: Input: x = 120 Output: 21
Python Solution
class Solution:
def reverse(self, x: int) -> int:
sign = 1 if x >= 0 else -1
x = abs(x)
result = 0
while x > 0:
digit = x % 10
if result > (2**31 - 1) // 10 or (result == (2**31 - 1) // 10 and digit > 7):
return 0
if result < -2**31 // 10 or (result == -2**31 // 10 and digit > 8):
return 0
result = result * 10 + digit
x //= 10
return sign * result
Java Solution
class Solution {
public int reverse(int x) {
int sign = x >= 0 ? 1 : -1;
x = Math.abs(x);
int result = 0;
while (x > 0) {
int digit = x % 10;
if (result > Integer.MAX_VALUE / 10 ||
(result == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
return 0;
}
if (result < Integer.MIN_VALUE / 10 ||
(result == Integer.MIN_VALUE / 10 && digit > -(Integer.MIN_VALUE % 10))) {
return 0;
}
result = result * 10 + digit;
x /= 10;
}
return sign * result;
}
}
C++ Solution
class Solution {
public:
int reverse(int x) {
int sign = x >= 0 ? 1 : -1;
x = abs(x);
int result = 0;
while (x > 0) {
int digit = x % 10;
if (result > INT_MAX / 10 ||
(result == INT_MAX / 10 && digit > INT_MAX % 10)) {
return 0;
}
if (result < INT_MIN / 10 ||
(result == INT_MIN / 10 && digit > -(INT_MIN % 10))) {
return 0;
}
result = result * 10 + digit;
x /= 10;
}
return sign * result;
}
};
JavaScript Solution
/**
* @param {number} x
* @return {number}
*/
var reverse = function(x) {
const sign = x >= 0 ? 1 : -1;
x = Math.abs(x);
let result = 0;
while (x > 0) {
const digit = x % 10;
if (result > Math.floor(Number.MAX_SAFE_INTEGER / 10) ||
(result === Math.floor(Number.MAX_SAFE_INTEGER / 10) &&
digit > Number.MAX_SAFE_INTEGER % 10)) {
return 0;
}
if (result < Math.floor(Number.MIN_SAFE_INTEGER / 10) ||
(result === Math.floor(Number.MIN_SAFE_INTEGER / 10) &&
digit > -(Number.MIN_SAFE_INTEGER % 10))) {
return 0;
}
result = result * 10 + digit;
x = Math.floor(x / 10);
}
return sign * result;
};
C# Solution
public class Solution {
public int Reverse(int x) {
int sign = x >= 0 ? 1 : -1;
x = Math.Abs(x);
int result = 0;
while (x > 0) {
int digit = x % 10;
if (result > int.MaxValue / 10 ||
(result == int.MaxValue / 10 && digit > int.MaxValue % 10)) {
return 0;
}
if (result < int.MinValue / 10 ||
(result == int.MinValue / 10 && digit > -(int.MinValue % 10))) {
return 0;
}
result = result * 10 + digit;
x /= 10;
}
return sign * result;
}
}
Complexity Analysis
- Time Complexity: O(log |x|) where |x| is the absolute value of the input number
- Space Complexity: O(1) as we only use a constant amount of extra space
Solution Explanation
This solution uses a digit-by-digit approach:
- First, we handle the sign of the number
- We work with the absolute value of the number
- For each digit:
- Get the last digit using modulo 10
- Check for overflow before multiplication
- Build the reversed number
- Remove the last digit by dividing by 10
- Finally, we apply the sign to the result
Key points:
- We handle overflow carefully at each step
- We work with the absolute value to simplify the logic
- We handle edge cases (0, negative numbers) properly
- The solution is efficient as we only need to process each digit once