Problème
Write the decimal digits of n in reverse order and return the resulting number. Zeros that end up at the front disappear, so 1200 becomes 21.
Implement reverse_number(n).
Input
n: A positive integer.
Output
Return the reversed number.
Contraintes
1 ≤ n ≤ 10¹⁸. The reversed value always fits a signed 64-bit integer.
Exemples
Entrée :
n = 352
Sortie attendue :
expected = 253
Entrée :
long long n = 352;
Sortie attendue :
long long expected = 253;
Entrée :
long n = 352;
Sortie attendue :
long expected = 253;
Explication
The digits 3, 5, 2 reversed are 2, 5, 3.
Entrée :
n = 1200
Sortie attendue :
expected = 21
Entrée :
long long n = 1200;
Sortie attendue :
long long expected = 21;
Entrée :
long n = 1200;
Sortie attendue :
long expected = 21;
Explication
Reversed digits are 0021; leading zeros are dropped, giving 21.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
The key is to build the result one digit at a time from the end of the number.
Indice 2
Use repeated remainder and division to extract digits in reverse order.
Indice 3
Leading zeros in the reversed form should disappear naturally, so be careful not to preserve them.
Complexité attendue
O(d) time, O(1) extra space