-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0233.py
More file actions
33 lines (30 loc) · 757 Bytes
/
q0233.py
File metadata and controls
33 lines (30 loc) · 757 Bytes
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
#!/usr/bin/python3
from typing import List
class Solution:
def countDigitOne(self, n: int) -> int:
base = 10
power = 1
while base <= n:
base *= 10
power += 1
base //= 10
power -= 1
result = 0
while n >= 10:
high = n // base
if high == 0:
pass
else:
result += high * base // 10 * power
if high == 1:
result += n % base + 1
else:
result += base
n = n % base
base //= 10
power -= 1
if n > 0:
result += 1
return result
result = Solution().countDigitOne(99)
print(result)