
直接硬搜就可以了,只是需要考虑k为0的情况。
public class Solution {
/*
* @param : An integer
* @param : An integer
* @return: An integer denote the count of digit k in 1..n
*/
public int digitCounts(int k, int n) {
int ans = (k==0 ? 1 : 0);
for(int i=0; i<=n; i++){
ans += resolve(i, k);
}
return ans;
}
private int resolve(int n, int m){
int res = 0;
while(n>0){
if(n%10==m) res++;
n/=10;
}
return res;
}
};
题目来源: http://www.lintcode.com/zh-cn/problem/digit-counts/#
原文:http://www.cnblogs.com/cc11001100/p/7898185.html