首页 > 其他 > 详细

LintCode题解之统计数字

时间:2017-11-26 10:27:23      阅读:151      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

直接硬搜就可以了,只是需要考虑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/#

LintCode题解之统计数字

原文:http://www.cnblogs.com/cc11001100/p/7898185.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!