Problem overview
The function reverseNumber takes a non-negative Int, n, and returns the number you get by writing its digits backwards. For 123 the answer is 321. For 120 the answer is 21: backwards the digits read 0, 2, 1, and a leading zero doesn't count. For 7 it is 7, and for 0 it is 0.
Approach
This combines two moves. You already know how to take the last digit off a number: num % 10 reads it and num / 10 drops it. Reversing means the digit that comes off first must become the first digit of the answer, then the next one comes after it, and so on.
To append a digit to a number being built, shift the number one place left by multiplying by 10, then add the digit. From 32 and the digit 1, 32 * 10 + 1 is 321. Because digits leave num from the right and join result on the right, the order flips by itself.
Step by step
- Copy
nintovar num, since the parameter can't be reassigned. - Create
var result, starting at 0. Ifnis 0, the loop never runs and 0 is correct. - Write a
whileloop that runs whilenum > 0. - Inside, set
resulttoresult * 10 + num % 10. - Still inside, set
numtonum / 10. The digit just moved is gone fromnum. - After the loop, return
result.
For n = 123:
| Turn | num after the turn | result after the turn |
|---|---|---|
| 1 | 12 | 3 |
| 2 | 1 | 32 |
| 3 | 0 | 321 |
Why the order comes out reversed
Taking digits from the right of num is the opposite of how you read it. Building result by appending each digit on the right then reproduces that opposite order. The first digit taken, the last one of n, is shifted left more times than any other digit as later digits arrive, so it ends up in the highest place. The same trick builds a number out of digits in any situation where you know the digits one at a time. Shifting by 10 works because the number is written in base 10: each place is worth ten times the place to its right.
The solution in Scala
class Solution {
def reverseNumber(n: Int): Int = {
var num = n
var result = 0
while (num > 0) {
result = result * 10 + num % 10
num = num / 10
}
return result
}
}
Details that matter in Scala:
- Operator precedence does the work in
result * 10 + num % 10: both*and%bind tighter than+, so it is(result * 10) + (num % 10). - Both
numandresultarevar, because they change on every turn. Intoverflow wraps silently in Scala. The lesson's inputs go up to 1000000000, whose reverse is 1, so they are safe. But a ten-digit number with a large last digit can overflow:reverseNumber(1999999999)returns 1410065399, not 9999999991, because that value is above the 32-bit limit.
Edge cases and common mistakes
- Trailing zeros. For 120 the first digit moved is 0, and
0 * 10 + 0is still 0, so the zero vanishes. The loop then givesresultas 2 and finally 21. - Zero.
reverseNumber(0)returns 0 without entering the loop. - Single digit. 7 gives 7.
- Multiplying after adding.
(result + num % 10) * 10shifts the new digit too and ends with an extra zero.
Testing
reverseNumber(123) -> 321
reverseNumber(120) -> 21
reverseNumber(7) -> 7
reverseNumber(0) -> 0
reverseNumber(1000000000) -> 1
