Skip to main content
LeetCode 233, Hard. Topics: Math, Dynamic Programming, Recursion. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 35 parametrized pytest cases, and a playground notebook:

Problem

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

Examples

Constraints

  • 0 <= n <= 10<sup>9</sup>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026