首页 > 其他 > 详细

[lintcode medium] digit counts

时间:2015-12-19 06:32:28      阅读:211      评论:0      收藏:0      [点我收藏+]

Digit Counts

Count the number of k‘s between 0 and n. k can be 0 - 9.

Example

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=1;
        if(n<10) return count;
        for(int i=10;i<=n;i++)
        {
            int num=i;
            while(num>0)
            {
              int digit=num%10;
              num=num/10;
              if(digit==k)
              {
                 count++;
              }
            }
        }
        return count;
    }
};

 

[lintcode medium] digit counts

原文:http://www.cnblogs.com/kittyamin/p/5058580.html

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