|
U
Pro6. Count Digits of an Integer
Easy
Problem Description
Given an integer N, count its total number of decimal digits without converting it to a string.
Note:
- •The integer 0 has exactly 1 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 N.
Output Format
Print the total number of decimal digits in N.
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^18Time 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
input =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit