Talentd
FleetCode

233. Number of Digit One

Hard39.9% acceptance6 companies

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:

Input: n = 13
Output: 6

Example 2:

Input: n = 0
Output: 0

Constraints:

  • 0 <= n <= 109

Hints open one at a time

Hint 1
Beware of overflow.
23:42
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countDigitOne(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
13
Input
0
Your code is saved on this device.Solution