IT常识
技术 Python PHP JavaScript IOS Android Java 数据库 资源 公众号 代码片段 github
  • IT常识
  • 技术

LintCode题解之统计数字

Posted 2020-10-15

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LintCode题解之统计数字相关的知识,希望对你有一定的参考价值。

 

直接硬搜就可以了,只是需要考虑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题解之统计数字的主要内容,如果未能解决你的问题,请参考以下文章

LintCode题解之最长单词

LintCode题解之斐波纳契数列

LintCode题解之判断是否为平方数之和

Lintcode15 Permutations solution 题解

Lintcode365 Count 1 in Binary solution 题解

精LintCode领扣算法问题答案:入门

(c)2006-2024 SYSTEM All Rights Reserved IT常识