Problem overview
The function reverseNumber receives a non-negative integer n and returns the number you get when its digits are written in reverse order. 123 becomes 321. Zeros at the end of n move to the front of the reversed number, where they don't count, so 120 becomes 21. A one-digit number stays the same, and 0 gives 0.
Approach
Take the digits off n from the right, and push each one onto the right end of a new number. The last digit of n comes off first, and it becomes the first digit of the result, so the order is reversed with no extra work.
Two tools do it, both with integers:
n % 10andn / 10take the last digit offn, as in any digit loop.result * 10 + digitappends a digit toresult: multiplying by 10 shifts the digits one place left and leaves a 0 at the end for the new one.32 * 10 + 1is321.
For n = 123, the values after each pass are:
| Pass | n afterwards | result |
|---|---|---|
| 1 | 12 | 3 |
| 2 | 1 | 32 |
| 3 | 0 | 321 |
For n = 120 the first digit moved is 0, and 0 * 10 + 0 is still 0, so it leaves no trace:
| Pass | n afterwards | result |
|---|---|---|
| 1 | 12 | 0 |
| 2 | 1 | 2 |
| 3 | 0 | 21 |
Why it works: after each pass, result holds the digits taken off so far, in the order they were taken, and n holds the digits still waiting. When n reaches 0, nothing is waiting and result holds them all, reversed.
Step by step
- Create
resultwith:=, starting at 0. No digit has been moved yet. Forn = 0the loop never runs and 0 is correct. - Loop while
n > 0, with aforthat has only a condition, since Go has nowhile. - Move the last digit: set
resulttoresult * 10 + n % 10. - Drop that digit from
nwithn = n / 10. - Return
result.
The solution in Go
func reverseNumber(n int) int {
result := 0
for n > 0 {
result = result * 10 + n % 10
n = n / 10
}
return result
}
Go details:
- Precedence.
*,/and%bind tighter than+, soresult * 10 + n % 10means(result * 10) + (n % 10), which is what the loop needs. - Room for the result. An
intis 64 bits in Go 1.22 on 64-bit systems, so reversing a ten-digit number can't overflow.reverseNumber(999999999)returns999999999.
The same reversal is how a palindrome number is checked: a number is a palindrome when it equals its reverse. With this function, reverseNumber(121) == 121 is true, while reverseNumber(123) == 123 and reverseNumber(10) == 10 are false. Notice the last one: because trailing zeros vanish, 10 reverses to 1.
Complexity
One pass per digit, so the time grows with the number of digits (at most ten here). The space is constant: two integers.
Edge cases and common mistakes
- Trailing zeros.
reverseNumber(120)is21, not021. An integer has no leading zeros to keep. - Zero.
reverseNumber(0)returns 0. - Largest input.
reverseNumber(1000000000)returns 1. - Swapping the two lines in the loop. If
nis divided first, the digit you add is the wrong one. - Forgetting the multiplication.
result + n % 10would just add the digits together, giving 6 for 123 instead of 321.
Testing
123gives 321.120gives 21.7gives 7.0gives 0.
