题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
# -*- coding:utf-8 -*-
class Solution:
def checkNum(self,x):
num=0
while x :
if (x%10)==1:
num+=1
x=x//10
return num
def NumberOf1Between1AndN_Solution(self, n):
if n==0:
return 0
elif 1<=n<10:
return 1
elif n==10:
return 2
elif 10<n<20:
return 1+n-10+1+1
elif n==20:
return 12
elif 21<=n<=100:
return (n-21)//10+13
cnt=0
for i in range(1,n+1):
cnt+=self.checkNum(i)
return cnt
# write code here
class Solution:
def checkNum(self,x):
num=0
while x :
if (x%10)==1:
num+=1
x=x//10
return num
def NumberOf1Between1AndN_Solution(self, n):
if n==0:
return 0
elif 1<=n<10:
return 1
elif n==10:
return 2
elif 10<n<20:
return 1+n-10+1+1
elif n==20:
return 12
elif 21<=n<=100:
return (n-21)//10+13
cnt=0
for i in range(1,n+1):
cnt+=self.checkNum(i)
return cnt
# write code here