lintcode-medium-Digit Counts
2016-03-18 13:09
363 查看
Count the number of k's between 0 and n. k can be 0 - 9.
if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's(1, 10, 11, 12)
if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's(1, 10, 11, 12)
class Solution { /* * param k : As description. * param n : As description. * return: An integer denote the count of digit k in 1..n */ public int digitCounts(int k, int n) { // write your code here int count = 0; for(int i = k ; i <= n; i++){ count += singleCount(i, k); } return count; } public int singleCount(int num, int k){ if(num == 0 && k == 0) return 1; int count = 0; while(num > 0){ if(num % 10 == k) count++; num /= 10; } return count; } };
相关文章推荐
- Apache流处理框架对比
- 【转】社区O2O的增量与存量,机会在哪?
- addChildViewController
- 不使用QT IDE 使用windows(vs) linux(eclipse) 下开发
- ubuntu设置开机启动图形应用程序,替换默认图形桌面
- 怎么判断sql文件的编码格式
- HDU 吉哥系列故事——临时工计划
- sublime 3 与git 与github三者之间协调合作
- python 多继承(新式类) 二
- Codeforces 620E New Year Tree
- Android 命名空间和自定义属性
- ssh证书登录
- 求出整数中1的个数
- ubuntu&redhat网络配置
- transition(动画属性)
- linux命令(6)crontab的用法和解析
- 人工智能革命:人类将永生或者灭绝
- Generics
- Adapter 适配器模式
- Django settings — Django 1.6 documentation