题解 | #整数中1出现的次数#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6?tpId=13&tqId=23272&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return int整型 # class Solution: def NumberOf1Between1AndN_Solution(self , n: int) -> int: # write code here count=0 ''' 先遍历从1到N的整数有哪些,然后将整数转化为字符串,遍历该整数的每一位,和1做比较,如果是1,count+1,如果不是,不做任何操作 ''' for i in range(1,n+1): for j in list(str(i)): if j=="1": count+=1 return count