【剑指offer】从1到n整数中1出现的次数(python)
数字->字符串->字符列表
list(str)将字符串转为字符列表
# -*- coding:utf-8 -*- class Solution: def NumberOf1Between1AndN_Solution(self, n): # write code here cnt = 0 while n > 0: string = str(n) chars = list(string) for i in range(len(chars)): if chars[i] == '1': cnt += 1 n -= 1 return cnt