Foundations Working with digits and divisors
27. Count the Digits of a Number
Given an integer n, return how many digits it has when it is written out in the usual way.
A negative number is written with a minus sign in front. The minus sign is not a digit, so −312 has 3 digits, just like 312. Zeros count like any other digit, so 70500 has 5 digits. The number 0 is written as a single digit, so it has 1 digit.
Example 1
Input: n = 70500
5Explanation: The digits are 7, 0, 5, 0 and 0. Every zero counts, the ones at the end included.
Example 2
Input: n = -312
3Explanation: The digits are 3, 1 and 2. The minus sign in front is not a digit.
Example 3
Input: n = 0
1Explanation: 0 is written with one digit.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
-9 × 1015 ≤ n ≤ 9 × 1015
Clarifying questions
What you would ask an interviewer before writing any code.