Practice Reverse Integer
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Reverse the decimal digits of signed 32-bit integer x while retaining its sign. Return zero if the reversed value would fall outside [-2^31, 2^31 - 1]. The exercise does not permit a 64-bit integer to hold an intermediate result. Approach: Take one digit at a time from x and append it to the result. Before multiplying the result by ten or adding the digit, compare against the applicable 32-bit boundary. Checking only after arithmetic is too late in languages where that operation overflows. Removing trailing zeroes follows naturally from integer arithmetic.
Source: Reverse Integer on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
x = -420
-24
Reverse the magnitude, discard the leading zero, and retain the negative sign.
# Example 2
x = 1534236469
0
The reversed value would be 9646324351, beyond 2^31 - 1.
Constraints
Related LeetCode Problems
Practice more problems similar to Reverse Integer.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.