U

6. Count Digits of an Integer

Easy

Problem Description

Given an integer NN, count its total number of decimal digits without converting it to a string.

Note:

  • •The integer 00 has exactly 11 digit.
  • •Negative integers have the same number of digits as their absolute value (the minus sign does not count as a digit).

Input Format

A single line containing an integer NN.

Output Format

Print the total number of decimal digits in NN.

Example 1:
Input:
12345
Output:
5
Explanation: The digits are 1, 2, 3, 4, 5, giving a count of 5.
Example 2:
Input:
0
Output:
1
Explanation: The value 0 consists of a single digit 0.
Example 3:
Input:
-987
Output:
3
Explanation: Taking the absolute value |-987| = 987, which has 3 digits: 9, 8, 7.
Constraints:
  • -10^18 <= N <= 10^18
  • Time complexity target: O(log10 |N|)
  • Auxiliary space target: O(1)
Follow up:Could you compute the digit count in O(1) time using logarithmic math (log10)?
1,180 Online