LintCode-Digit Counts

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

Example

if n=12, in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1‘s (1, 10, 11, 12)

Analysis:

Method 1: directly count every number.

Method 2: Analytical solution.

Every time, calculate how many k has appears on a specific postion, i.e., on 1, 10, 100,.....

Solution 1:

 1 class Solution {
 2     /*
 3      * param k : As description.
 4      * param n : As description.
 5      * return: An integer denote the count of digit k in 1..n
 6      */
 7     public int digitCounts(int k, int n) {
 8         int[] record = new int[10];
 9         Arrays.fill(record,0);
10         for (int i=0;i<=n;i++){
11             String temp = Integer.toString(i);
12             for (int j=0;j<temp.length();j++){
13                 int ind = (int) (temp.charAt(j)-‘0‘);
14                 record[ind]++;
15             }
16         }
17         return record[k];
18
19     }
20 };

Solution 2:

 1 class Solution {
 2     /*
 3      * param k : As description.
 4      * param n : As description.
 5      * return: An integer denote the count of digit k in 1..n
 6      */
 7     public int digitCounts(int k, int n) {
 8         int res = 0;
 9         int base = 1;
10         while (base<=n){
11             int part1 = n/(base*10);
12             if (base>1 && k==0 && part1>0) part1--;
13             part1 *= base;
14             int bar = n/base%10;
15             int part2 = 0;
16             if (k<bar) part2 = base;
17             else if (k==bar) part2 = n%base+1;
18             if (k==0 && n<base*10) part2 = 0;
19             res += part1+part2;
20             base*=10;
21         }
22         return res;
23     }
24 };
时间: 2024-07-29 06:08:40

LintCode-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

【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)

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.

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当

[Algorithm] 3. Digit Counts

Description 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) Answer /** * @param k: An integer * @param n: An integer * @return: An intege

欧拉工程第63题:Powerful digit counts

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

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