[Algorithm] 3. Digit Counts

Description

Count the number of k‘s between 0 and nk 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)

Answer



    /**
     * @param k: An integer
     * @param n: An integer
     * @return: An integer denote the count of digit k in 1..n
     */
    int digitCounts(int k, int n) {
        // Check every digit from 0 to n.
        int count, temp, i = 0;
        for ( i=0; i<=n; i++ )
        {
            temp = i;
            do
            {
                // Check the ones and tens place respectively for the digits between 10 to 99.
                if ( (temp >= 10) && (temp < 100) ) {
                    if ( (temp/10) == k )   count++;
                    if ( (temp%10) ==k )    count++;
                } else {    // Check the ones place only for other cases.
                    if ( (temp%10) == k )
                        count++;
                }

                temp /= 10;
            } while (temp >= 10);
        }

        return count;
    }

 

原文地址:https://www.cnblogs.com/jjlovezz/p/9915780.html

时间: 2024-10-29 04:46:50

[Algorithm] 3. Digit Counts的相关文章

[lintcode medium] digit counts

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. * r

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) 分析: 利用while 循环 + num % 10可以获取 num中的所有数字. class Solution { /* * param k : As description.

【Lintcode】003.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) 题解: Solution 1 () class Solution { public: int digitCounts(int k, int n) { if (n < 0)

欧拉工程第63题:Powerful digit counts

题目链接 这个题目有点坑: 先说自己的思路<虽然走不通> 根据题意可知道: a的b次方 除以 最小的b位数(如:10,100,1000) 的商 在 1--9之间,则:a的b次方就是符合题意的 然后就根据这个遍历 先找到第一个数符合条件的数firstnum 再找到第一个符合条件之后的第一个不满足条件的数nextnum 则:这中间有 nextnum - firstnum个数 当b也就是次方数大于18的时候,Long都溢出了 此时:有38个数

Lintcode3 Digit Counts solution 题解

[题目描述] Count the number of k's between 0 and n. k can be 0 - 9. 计算数字k在0到n中的出现的次数,k可能是0~9的一个值. [题目链接] http://www.lintcode.com/en/problem/digit-counts/ [题目解析] 方法一: Brute Force, 0到n个数挨个算过去.最大的问题就是效率,当n非常大时,就需要很长的运行时间. 方法二:当某一位的数字小于i时,那么该位出现i的次数为:更高位数字x当

Project Euler:Problem 63 Powerful digit counts

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power. How many n-digit positive integers exist which are also an nth power? 这样的数字满足以下条件: 对于数位为x的数S=k^x 有 10^(x-1)<=k^x<=10^x-1 #include &qu

Leetcode: Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

LintCode &quot;Permutation Index II&quot; !

Simply a variation to "Permutation Index". When calculating current digit index, we consider duplicated case. Again, similar as "Digit Counts", it is another counting problem and stil digit by digit. And please note: we can use Fenwick