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
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026