Einfach 0 gelöst 0% Erfolgsquote

Count Digits

Count the decimal digits of a positive integer.

Das lernst du

  • Count how many times a number can be divided by 10
  • Use a counter inside a while loop
  • Treat single-digit numbers as an edge case

Problem

Übersetzung anzeigen

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

Beispiel 1
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.

Beispiel 2
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

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.