Lintcode : 统计数字
来源:互联网 发布:保存json文件 编辑:程序博客网 时间:2024/05/12 04:49
统计数字
计算数字k在0到n中的出现的次数,k可能是0~9的一个值
您在真实的面试中是否遇到过这个题?
Yes
样例
标签 Expand 例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)
相关题目 Expand
Timer Expand
当某一位的数字小于i时,那么该位出现i的次数为:更高位数字x当前位数当某一位的数字等于i时,那么该位出现i的次数为:更高位数字x当前位数+低位数字+1当某一位的数字大于i时,那么该位出现i的次数为:(更高位数字+1)x当前位数
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 hereint res = 0;int base = 1;if(n==0&&k==0) return 1;while(n/base>0){int curBit = (n/base)%10;int low = n - (n/base)*base; int high = n/(base*10);if (curBit < k) {res += high*base;} else if (curBit == k) {res += high*base+low+1;} else {if(k==0&&high==0){}else{res += (high+1)*base;}}base *=10;}return res; }};
0 0
- Lintcode : 统计数字
- lintcode ----统计数字
- lintcode-3-统计数字
- LintCode 3 统计数字
- LintCode 3 统计数字
- LintCode Python 统计数字
- [LintCode]3.统计数字
- LintCode 3.统计数字
- LintCode-(3)统计数字
- LintCode之3 统计数字
- Lintcode 3统计数字
- Lintcode 3. 统计数字
- lintcode 3:统计数字
- LintCode-剑指Offer-(3)统计数字
- (lintcode)第3题统计数字
- LintCode笔记(7)——统计数字
- 【LintCode】3.统计数字(Medium)解题报告
- lintcode 3. 统计数字 纯数学解法
- Intellij_idea-14官方快捷键中文版
- 条形码识别(4)——校验
- GTK+构件
- 程序的优雅
- JS视图搜索功能(将检索条件赋到url中传值)
- Lintcode : 统计数字
- git的常用方法
- Java内存溢出的详细解决方案
- EditText自定义样式
- 学习笔记3
- CRect:返回一些列的矩形区域
- 求无序数组之中最小的k个数
- tomcat+jenks环境自动部署脚本
- 【高精度算法】A*B