Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

Example 1
Inputn = 13
Output6
From 0 to 13, the digit 1 appears in 1, 10, 11, 12, and 13 for a total of 6 times.
Example 2
Inputn = 0
Output0
There are no digit 1 occurrences in the only number 0.

Constraints

  • 0 <= n <= 10^9

Asked at 6 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate