[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.
     * 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;
    }
};
时间: 2024-12-20 01:09:09

[lintcode medium] digit counts的相关文章

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

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[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个数

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution