Reverse Integer
1 approach, code in every language
Problem description
Given a signed 32-bit integer, produce the integer whose decimal digits are written in the opposite order. The sign of the value is preserved, and any trailing zeros of the original number simply vanish once they move to the front.
Because the computing environment can only hold signed 32-bit integers, the reversed value might not fit. If the reversal falls outside the range that a 32-bit signed integer can represent, return 0 instead.
Example 1
Input: x = 456
Output: 654
The digits 4, 5, 6 are emitted in reverse to form 654.
Example 2
Input: x = 1999999999
Output: 0
Reversing yields 9999999991, which exceeds the largest 32-bit signed integer, so the answer is 0.
Constraints
-2^31 <= x <= 2^31 - 1The reversed value must fit in a signed 32-bit integer, otherwise return 0Assume the environment cannot store any integer wider than 32 bits
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Legacy Register Flip is our own problem on the technique behind Reverse Integer: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free