forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-digit-one.py
39 lines (32 loc) · 1.04 KB
/
number-of-digit-one.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Time: O(logn)
# Space: O(1)
#
# Given an integer n, count the total number of digit 1 appearing
# in all non-negative integers less than or equal to n.
#
# For example:
# Given n = 13,
# Return 6, because digit 1 occurred in the following numbers:
# 1, 10, 11, 12, 13.
#
class Solution:
# @param {integer} n
# @return {integer}
def countDigitOne(self, n):
k = 1;
cnt, multiplier, left_part = 0, 1, n
while left_part > 0:
# split number into: left_part, curr, right_part
curr = left_part % 10
right_part = n % multiplier
# count of (c000 ~ oooc000) = (ooo + (k < curr)? 1 : 0) * 1000
cnt += (left_part / 10 + (k < curr)) * multiplier
# if k == 0, oooc000 = (ooo - 1) * 1000
if k == 0 and multiplier > 1:
cnt -= multiplier
# count of (oook000 ~ oookxxx): count += xxx + 1
if curr == k:
cnt += right_part + 1
left_part /= 10
multiplier *= 10
return cnt