首页 > 编程语言 > 详细

【剑指offer】Q32:从1到n整数1出现的次数(python)

时间:2014-07-03 16:04:32      阅读:456      评论:0      收藏:0      [点我收藏+]
def q32(n, len):
	if n < 0:
		return 0
	elif n <= 1:
		return n
	total = 0	
	while n > 0:
		if  n >= 1 and n < 10:
			total += 1
			return total
		p1 = n % (10**(len - 1))
		h = n / (10**(len - 1))
		p2 = p1 + 1
		n = p1
		if h > 1:
			total += 10**(len - 1)
		else:
			total += p2
		total += 10 **(len -2) * h * (len - 1)	
	return total

【剑指offer】Q32:从1到n整数1出现的次数(python),布布扣,bubuko.com

【剑指offer】Q32:从1到n整数1出现的次数(python)

原文:http://blog.csdn.net/shiquxinkong/article/details/36523903

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!