Problem overview
The function reverse_number takes a non-negative n, at most 1_000_000_000, and returns the number with its digits in reverse order. Zeros at the end of n move to the front, where they don't count: 120 reversed is 21. The reverse of 0 is 0.
For n = 123 the answer is 321, and a one-digit number stays the same.
Approach
The last digit of n is n % 10, and n / 10 removes it. The digits come off from the right, and the last digit of n is the first digit of the answer, so the order is already the one you need.
To build the answer, keep a variable result. For each digit taken off, multiply result by 10, which shifts its digits one place to the left and leaves a 0 at the end, then add the new digit there. For 123:
| pass | num after the pass | result after the pass |
|---|---|---|
| 1 | 12 | 3 |
| 2 | 1 | 32 |
| 3 | 0 | 321 |
Trailing zeros take care of themselves. For 120 the first digit moved is 0, and 0 * 10 + 0 is still 0, so result stays 0 until the 2 arrives. The passes end with result at 21.
Step by step
- Copy
nintonumwithlet mut. The loop must change it, and parameters can't be changed. - Create
resultat 0. It is mutable. Ifnis 0 the loop never runs, and 0 is the right answer. - Loop while
num > 0. The last digit comes off first. - Set
resulttoresult * 10 + num % 10. That shifts the digits built so far and appends the new one. - Replace
numwithnum / 10to drop the digit that was just moved. - Return
resultas the last expression.
The solution in Rust
impl Solution {
fn reverse_number(n: i32) -> i32 {
let mut num = n;
let mut result = 0;
while num > 0 {
result = result * 10 + num % 10;
num = num / 10;
}
result
}
}
- Operator precedence.
*,/and%bind tighter than+, soresult * 10 + num % 10means(result * 10) + (num % 10). i32range. Every input up to1_000_000_000reverses to a value that fits in ani32(999_999_999stays999_999_999), so the lesson's inputs are safe. The largesti32is 2,147,483,647.- Overflow wraps in an optimized build. The lessons build with
-O, where integer overflow wraps silently. A debug build panics instead. Withn = 1_999_999_999, whose reverse doesn't fit, an optimized build returns 1410065399 rather than an error. That input is outside the lesson's limits, but it shows why a reversal can need a wider type such asi64in general.
Tracing 120
The trailing zero is the case people worry about, so here is the state after each pass for n = 120:
| pass | num after the pass | result after the pass |
|---|---|---|
| 1 | 12 | 0 |
| 2 | 1 | 2 |
| 3 | 0 | 21 |
No special handling is needed: the leading zero of 021 simply never exists as a digit of an integer.
Edge cases and common mistakes
- Trailing zeros.
120gives 21, and1_000_000_000gives 1. - Zero. The loop doesn't run, and the function returns 0.
- One digit.
7gives 7. - Swapping the two updates. If
numis divided beforenum % 10is used, the digit that was meant forresultis already gone. - Forgetting the
* 10. Just adding the digit gives the digit sum, not the reversal.
Testing
n = 123gives 321.n = 120gives 21.n = 7gives 7.n = 0gives 0.n = 1000000000gives 1.n = 999999999gives 999999999.
