python 整数中1出现的次数

把整数转换为字符串

用count计数

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def NumberOf1Between1AndN_Solution(self, n):
 4         # write code here
 5         count = 0
 6         for i in range(1,n+1):
 7             s = str(i)
 8             count += s.count('1')
 9         return count
10         

 

posted @ 2018-03-11 19:59  shunyu  阅读(269)  评论(0编辑  收藏  举报