Problem
Count how many decimal digits n has when written normally, without leading zeros.
Implement count_digits(n).
Input
n: A positive integer.
Output
Return the number of digits.
Einschränkungen
1 ≤ n ≤ 10¹⁸
Beispiele
Eingabe:
n = 352
Erwartete Ausgabe:
expected = 3
Eingabe:
long long n = 352;
Erwartete Ausgabe:
long long expected = 3;
Eingabe:
long n = 352;
Erwartete Ausgabe:
long expected = 3;
Erläuterung
352 has three digits.
Eingabe:
n = 7
Erwartete Ausgabe:
expected = 1
Eingabe:
long long n = 7;
Erwartete Ausgabe:
long long expected = 1;
Eingabe:
long n = 7;
Erwartete Ausgabe:
long expected = 1;
Erläuterung
A single digit.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
You only need to know how many times you can remove a digit before nothing remains.
Hinweis 2
A loop with division by 10 or a string length both work well.
Hinweis 3
Don’t forget that any positive single-digit number still has exactly one digit.
Erwartete Komplexität
O(d) time, O(1) extra space