十进制数中1的个数
Problem
Solution 1
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
i = 1
cnt = 0
while (n//i) != 0:
now = n//i%10 #当前位
left = n//(i*10) #高位
right = n%i #低位
if now == 0:
cnt+= left*i
elif now == 1:
cnt+= left*i + right+1
else:
cnt+= (left+1)*i
i = i*10
return(cnt)Solution 2 (Python抖机灵版)
Last updated