1837. Digit Span
by Rohit Parihar2 submissions
A printing shop prices a number by how many characters its digits take on the page. Given the integer n, return how many decimal digits it has. A minus sign is not a digit, so -90 has two digits. The number 0 is written with a single digit.
Do not convert the number to text: remove the last digit with integer division and count how many times you can do it. Take care with negative numbers and with 0, which must give 1, not 0. Aim for O(log n) time and O(1) extra space.
Example 1
- Input:
- n = 1234
- Output:
- 4
- Explanation:
1, 2, 3 and 4 are four digits.
Example 2
- Input:
- n = -90
- Output:
- 2
- Explanation:
The minus sign does not count, so there are two digits.
Example 3
- Input:
- n = 0
- Output:
- 1
- Explanation:
Zero is written with one digit.
Constraints
-1015 ≤ n ≤ 1015
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(log n)
- Space
- O(1)