Lesson learnt: one effective solution for bit\digit counting problems: counting by digit\bit

http://www.hawstein.com/posts/20.4.html

class Solution {
public:
/*
* param k : As description.
* param n : As description.
* return: How many k's between 0 and n.
*/
int digitCounts(int k, int n) {
int ret = ;
int base = ;
while (n/base > )
{
int cur = (n/base) % ;
int low = n - (n/base) *base;
int high= n / (base * ); ret += high * base; // at least this many
if (cur == k)
{
ret += low + ; // all k-xxxxx
}
else if(cur > k)
{
if(!(!k && base > )) // in case of 0
ret += base; // one more base
}
base *= ;
}
return ret;
}
};

LintCode "Digit Counts" !!的更多相关文章

  1. 【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 ...

  2. 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, ...

  3. [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, ...

  4. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...

  5. 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 ...

  6. 3. Digit Counts【medium】

    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, ...

  7. Project Euler 63: Powerful digit counts

    五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 剑指offer ------ 刷题总结

    面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...

随机推荐

  1. Java中生成随机字符的方法总结

    package learnExercise; public class RandomCharacter { public static char getRandomCharacter(char ch1 ...

  2. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  3. 使用StringBuilder更高效的处理字符串

    前言 本文介绍一个java中处理字符串的技巧 - 使用StringBuilder提高字符串处理效率. StringBuilder 当程序要频繁的进行字符串拼接的时候,直接使用String效率会比较低. ...

  4. Java-->实现群聊功能(C/S模式--TCP协议)

    --> Java 对TCP协议的支持: --> java.net包中定义了两个类ServerSocket 和Socket ,分别用来实现双向连接的server 端和client 端. -- ...

  5. ES6-Symbol

    javaScript的数据类型:number,string,boolean,undefined,null,object ES6带来了一个新的数据类型:symbol 目的是:解决对象的属性名冲突的问题. ...

  6. leetcode 139. Word Break ----- java

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. MFC-CString 字符串分割

    CString strSrc = _T("1++2+3+4"); CStringArray strResult; CString strGap = _T("+" ...

  8. JQ插件jquery.fn.extend与jquery.extend

    jQuery为开发插件提拱了两个方法,分别是: JavaScript代码 jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend( ...

  9. 系统镜像以及微PE工具箱

    微PE地址:http://www.wepe.com.cn/download.html MSDN镜像下载地址:http://msdn.itellyou.cn/ 小白也能轻松装系统(win10 64位) ...

  10. myeclipse10安装egit和使用

    一.下载egit插件并安装到eclipse 下载egit插件包,然后解压放到Eclipse的dropins文件夹内或者直接放到对应的文件夹下 二.安装成功(window->preferences ...