All problems
1098HardMathDynamic ProgrammingRecursion

Ones Printed Across the Page Numbers

Tracked in this browser only
Write code

Trains the technique from

LeetCode 233Number of Digit One

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A signwriter numbers pages from 1 up to n, printing each number in ordinary decimal with no leading zeros. When n is 0 nothing is printed at all.

Return how many times the digit 1 is printed across all those page numbers.

Examples

Example 1

Input
n = 25
Output
13

The units place carries a 1 on pages 1, 11 and 21. The tens place carries one on every page from 10 through 19. That is thirteen printed digits.

Example 2

Input
n = 9
Output
1

Among the single-digit pages only page 1 carries the digit.

Example 3

Input
n = 99
Output
20

Ten pages carry a 1 in the tens place and ten pages carry one in the units place, so twenty digits are printed.

Constraints

  • 0 <= n <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_digit_one(n: int) -> int:
Java
public int countDigitOne(int n)
September 7
Apply