問題
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.
制約
1 ≤ n ≤ 10¹⁸
例
例 1
入力:
n = 352
期待される出力:
expected = 5
入力:
long long n = 352;
期待される出力:
long long expected = 5;
入力:
long n = 352;
期待される出力:
long expected = 5;
解説
The digits are 3, 5 and 2; the largest is 5.
例 2
入力:
n = 7
期待される出力:
expected = 7
入力:
long long n = 7;
期待される出力:
long long expected = 7;
入力:
long n = 7;
期待される出力:
long expected = 7;
解説
The only digit is 7.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Compare every decimal digit and keep the biggest one seen so far.
ヒント 2
Extract digits one by one from the right, or inspect the string characters.
ヒント 3
A number containing 9 anywhere immediately sets the answer to 9.
目安の計算量
O(d) time, O(1) extra space