문제
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.
제약 조건
1 ≤ n ≤ 10¹⁸
예시
예시 1
입력:
n = 352
예상 출력:
expected = 3
입력:
long long n = 352;
예상 출력:
long long expected = 3;
입력:
long n = 352;
예상 출력:
long expected = 3;
설명
352 has three digits.
예시 2
입력:
n = 7
예상 출력:
expected = 1
입력:
long long n = 7;
예상 출력:
long long expected = 1;
입력:
long n = 7;
예상 출력:
long expected = 1;
설명
A single digit.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
You only need to know how many times you can remove a digit before nothing remains.
힌트 2
A loop with division by 10 or a string length both work well.
힌트 3
Don’t forget that any positive single-digit number still has exactly one digit.
예상 복잡도
O(d) time, O(1) extra space