Reverse Integer - Problem
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).
Input & Output
Example 1 — Basic Positive Number
$
Input:
x = 123
›
Output:
321
💡 Note:
Reverse the digits: 123 becomes 321. The result fits in 32-bit range so return 321.
Example 2 — Negative Number
$
Input:
x = -123
›
Output:
-321
💡 Note:
Reverse the digits while preserving the negative sign: -123 becomes -321.
Example 3 — Overflow Case
$
Input:
x = 1534236469
›
Output:
0
💡 Note:
Reversing gives 9646324351, which exceeds 2³¹-1 = 2147483647, so return 0.
Constraints
- -2³¹ ≤ x ≤ 2³¹ - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
32-bit signed integer x
2
Process
Reverse digits while checking overflow
3
Output
Reversed integer or 0 if overflow
Key Takeaway
🎯 Key Insight: Check for overflow before multiplying by 10 to avoid using 64-bit integers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code