File tree 1 file changed +4
-4
lines changed
scripts/algorithms/N/Number of Digit One
1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 65 ms (Top 7.05%) | Memory: 13.8 MB (Top 68.36%)
1
2
class Solution :
2
3
def countDigitOne (self , n : int ) -> int :
3
4
num = str (n )[::- 1 ]
4
5
count = 0
5
6
for i in range (len (num )- 1 , - 1 , - 1 ):
6
- pv = 10 ** i # placevalue
7
+ pv = 10 ** i # placevalue
7
8
# mulitplicity of current digit (how many times it will be repeated)
8
9
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
11
12
# if the current digit is greater than 1 then additional number of 1's are added to the count (equivalent to the place value)
12
13
if num [i ] > '1' :
13
14
count += pv
14
15
# 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)
15
16
if num [i ] == '1' :
16
17
count += rem + 1
17
18
return count
18
-
You can’t perform that action at this time.
0 commit comments