LeetCode 233 Number of Digit One
2015-12-11 10:32
561 查看
题目描述
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.
Hint:
Beware of overflow.
分析
参考:http://blog.csdn.net/xudli/article/details/46798619代码
[code] public int countDigitOne(int n) { int ones = 0; for (long m = 1; m <= n; m *= 10) { long a = n / m, b = n % m; ones += (a + 8) / 10 * m; if (a % 10 == 1) ones += b + 1; } return ones; }
相关文章推荐
- Python性能优化
- 你应该知道的RPC原理
- 前端,移动开发者,UI须懂: 不同设备的之间的尺寸
- 数据库设计的技巧
- java微信公众号开发总结(1)——接口认证
- CAS认证系统基础
- 11月流量入口占比动态:直接访问夺冠 占比持续下降
- 升级wordpress到4.4之后出现Uncaught TypeError Cannot read property WordCounter
- 浏览器缩小出现滚动条
- Java之I/O以及其它主题
- CSS权威指南学习笔记--Chapter4 值和单位
- iOS微信安装包瘦身
- 健康说网页设计
- Django中URL的解析和反查
- 抓取网页上的图片信息
- sql优化
- 解决dwr报错【 Error: java.lang.SecurityException: No class by name: service】
- logback日志文件的使用
- 8.shell中文本处理三剑客
- RecycleView的简单应用