Problem overview
The function reverseNumber receives a non-negative integer n and returns the number you get by writing its digits in reverse order. For 123 the answer is 321. For 120 the digits read backwards are 0, 2, 1, which is the number 21, because a leading zero doesn't count. A one-digit number stays the same, and 0 gives 0.
Approach
Take the digits off n from the right with n % 10 and n // 10, and put each one on the end of a new number, result. The last digit of n comes off first, and it should be the first digit of the answer, so the order is already right.
To put a digit on the end of result, shift result one place left by multiplying by 10, which leaves a 0 at the end, then add the digit. Starting from 32 and moving the digit 1: 32 * 10 + 1 is 321.
Step by step
- Start
resultat 0. Nothing has moved yet. Forn = 0the loop never runs and 0 is right. - Loop while
n > 0. The loop ends when every digit has been taken off. - Set
resulttoresult * 10 + n % 10. This shifts the digits built so far and appends the last digit ofn. - Set
nton // 10. The digit is now inresult, so it can be dropped. - Return
resultafter the loop.
The loop on n = 123:
| n at the start | result before | result after | n after dropping |
|---|---|---|---|
| 123 | 0 | 3 | 12 |
| 12 | 3 | 32 | 1 |
| 1 | 32 | 321 | 0 |
And on n = 120:
| n at the start | result before | result after | n after dropping |
|---|---|---|---|
| 120 | 0 | 0 | 12 |
| 12 | 0 | 2 | 1 |
| 1 | 2 | 21 | 0 |
The solution in Python
class Solution:
def reverseNumber(self, n: int) -> int:
result = 0
while n > 0:
result = result * 10 + n % 10
n = n // 10
return result
Details worth knowing:
- Operator precedence. In
result * 10 + n % 10, both*and%bind tighter than+, so it means(result * 10) + (n % 10). - Why trailing zeros vanish. For 120 the first digit moved is 0, and
0 * 10 + 0is still 0. Nothing marks the zero as a digit until a nonzero digit comes after it. That matches the lesson's rule that leading zeros of the answer don't count. - The string version.
int(str(120)[::-1])also gives 21: the slice[::-1]reverses the text"120"into"021", andintreads that as 21. The loop does the same with arithmetic only. - No overflow. Python integers grow as needed, so even a reversed number that is larger than the input is safe.
Why multiply by 10
Each place in a number is worth ten times the place to its right. The number 321 is 3 * 100 + 2 * 10 + 1. When a new digit arrives on the right, every digit already in result moves one place to the left, which is a multiplication by 10, and the new digit takes the units place. Applying the same step to each digit that comes off n builds the reversed number from its first digit to its last.
The reversal loses information when the input ends in zeros: reversing 120 gives 21, and reversing 21 gives 12, not 120. So reversing twice returns the original number only when it has no trailing zeros. For 472, reversing twice does give 472.
Edge cases and common mistakes
- Zero.
reverseNumber(0)returns 0. - Numbers ending in zeros.
1000and1000000000both give 1. - Multiplying after adding.
(result + n % 10) * 10adds a stray zero at the end of the answer. - Forgetting
n = n // 10. The loop would never end.
Testing
123gives321.120gives21.7gives7.0gives0.1000gives1.
