-
Notifications
You must be signed in to change notification settings - Fork 4
/
整数中1出现的次数.py
32 lines (30 loc) · 913 Bytes
/
整数中1出现的次数.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
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
#循环的出口
preceise = 1
highValue = 1
midValue = 1
lowValue = 1
count = 0
sumNum = 0
while highValue != 0:
highValue = n // (preceise * 10)
midValue = (n // preceise) % 10
lowValue = n % preceise
preceise = preceise * 10
if midValue == 0:
#借一位
num = (highValue -1 +1) * pow(10, count)
elif midValue > 1:
num = (highValue +1) * pow(10, count)
else:
num = (highValue) * pow(10, count) + lowValue + 1
sumNum += num
count += 1
return sumNum
if __name__ == '__main__':
s = Solution()
numbers = 30
ans = s.NumberOf1Between1AndN_Solution(numbers)
print(ans)