Problème
Look at every decimal digit of n and return the largest one.
Implement largest_digit(n).
Input
n: A positive integer.
Output
Return the largest digit, from 0 to 9.
Contraintes
1 ≤ n ≤ 10¹⁸
Exemples
Entrée :
n = 352
Sortie attendue :
expected = 5
Entrée :
long long n = 352;
Sortie attendue :
long long expected = 5;
Entrée :
long n = 352;
Sortie attendue :
long expected = 5;
Explication
The digits are 3, 5 and 2; the largest is 5.
Entrée :
n = 7
Sortie attendue :
expected = 7
Entrée :
long long n = 7;
Sortie attendue :
long long expected = 7;
Entrée :
long n = 7;
Sortie attendue :
long expected = 7;
Explication
The only digit is 7.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Compare every decimal digit and keep the biggest one seen so far.
Indice 2
Extract digits one by one from the right, or inspect the string characters.
Indice 3
A number containing 9 anywhere immediately sets the answer to 9.
Complexité attendue
O(d) time, O(1) extra space