233. Number of Digit One【H】【33】【再来一遍】
来源:互联网 发布:淘宝仓库怎么看 编辑:程序博客网 时间:2024/05/22 01:39
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
Subscribe to see which companies asked this question
按位算,先算个位数的1 有多少个,再算十位数的,依次往上
写了好多遍啊。。。。
class Solution(object): def countDigitOne(self, n): if n < 1: return 0 b = 0 t = 1 res = 0 while t <= n: b = n / (t*10) tres = b * t temp = (n / t)%10 if temp == 10: temp = 1 if temp == 1: tres += n % t + 1 elif temp > 1: tres += t res += tres #print t,b,tres t *= 10 return int(res)
0 0
- 233. Number of Digit One【H】【33】【再来一遍】
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- SynapticsTouchDriver 思维导图
- MySQL学习笔记二
- 2016.5.21【初中部 NOIP提高组 】模拟赛A 总结
- startService与bindService的区别
- 刻在英国威斯敏斯特大教堂的一块墓碑上的话
- 233. Number of Digit One【H】【33】【再来一遍】
- nanopi引脚定义
- 链接器脚本lds
- 【学生信息管理系统】总结二
- android调试输出log打印信息到本地文件
- 机器学习入门学习资源
- eclipse导入tomcat 8.0x源码
- 汇编代码中lea指令的另一种使用
- TCP/IP入门(1) --链路层