Skip to content

Commit 34b2c80

Browse files
committed
Runtime: 65 ms (Top 7.05%) | Memory: 13.8 MB (Top 68.36%)
1 parent b0f3b99 commit 34b2c80

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
# Runtime: 65 ms (Top 7.05%) | Memory: 13.8 MB (Top 68.36%)
12
class Solution:
23
def countDigitOne(self, n: int) -> int:
34
num = str(n)[::-1]
45
count = 0
56
for i in range(len(num)-1, -1, -1):
6-
pv = 10**i # placevalue
7+
pv = 10**i # placevalue
78
# mulitplicity of current digit (how many times it will be repeated)
89
mul = n//(pv*10)
9-
rem = n % pv # remainder of current place value
10-
count += mul * pv # count for number of times 1 occurs in this place when the current digit is considered to be less than 1
10+
rem = n % pv # remainder of current place value
11+
count += mul * pv # count for number of times 1 occurs in this place when the current digit is considered to be less than 1
1112
# if the current digit is greater than 1 then additional number of 1's are added to the count (equivalent to the place value)
1213
if num[i] > '1':
1314
count += pv
1415
# if the current digit is equal to 1 then additional number of 1's are added to the count (equivalent to the number modded by the current place value)
1516
if num[i] == '1':
1617
count += rem + 1
1718
return count
18-

0 commit comments

Comments
 (0)