LintCode刷题记录

3.统计数字

计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值。

class Solution:
"""
@param k: An integer
@param n: An integer
@return: An integer denote the count of digit k in 1..n
"""
def digitCounts(self, k, n):
# write your code here
times = 0

for i in range(n+1):
item = str(i)
while len(item) > 0:
if item[0:1] == str(k):
times += 1
item = item[1:]
return times

 

猜你喜欢

转载自www.cnblogs.com/longchaos/p/10420627.html
今日推荐