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. System.out.println调试输出

    Android开发中在代码中通过System.out.println调试输出在Logcat窗口中可以看到. 但Logcat视图中夹杂了太多的其它App及底层的信息,看起来并不明朗.可以在Logcat视 ...

  2. Handler 引起的内存泄露

    先看一组简单的代码 1 2 3 4 5 6 7 8 9 public class SampleActivity extends Activity { private final Handler mHa ...

  3. discuz如何设置游客可以浏览板块,但是不能查看帖子内容呢?

    后台 ,将用户组 --游客 阅读权限设置为0 QQ快捷登陆不正常 ,在首页,点击QQ快捷登陆,并不跳转到QQ认证页面,而是要输入用户名.密码.验证码等. 但是如果输入的是你想新注册的用户名等信息,却不 ...

  4. CSS控制文本超出指定宽度后用省略号代替,CSS控制文本不换行

    CSS控制文本超出指定宽度后用省略号代替,CSS控制文本不换行. 一般的文字截断(适用于内联与块): .text-overflow {     display:block;/*内联对象需加*/     ...

  5. linux之df命令

    介绍: Linux中df命令可以用来显示目前在Linux系统上的文件系统的磁盘使用情况统计.这些工具可以方便地知道哪些文件系统消耗多少内存.此外,如果被拾起,并且提供一个特定的文件名作为df命令的参数 ...

  6. phpwind8.7升级9.0.1过程(四)20130207升级到20141228

    每一次升级前都要注意备份 1.网站根目录的所有文件 2.网站的数据库 根据phpwind官方教程 更新到20130702版本成功并备份 更新到20140428版本成功并备份 20141228版本的更新 ...

  7. poj1236 强连通

    题意:有 n 个学校每个学校可以将自己的软件共享给其他一些学校,首先,询问至少将软件派发给多少学校能够使软件传播到所有学校,其次,询问添加多少学校共享关系可以使所有学校的软件能够相互传达. 首先,第一 ...

  8. hdu3861 强连通+最小路径覆盖

    题意:有 n 个点,m 条边的有向图,需要将这些点分成多个块,要求:如果两点之间有路径能够互相到达,那么这两个点必须分在同一块:在同一块内的任意两点相互之间至少要有一条路径到达,即 u 到达 v 或 ...

  9. 越狱Season 1-Episode 1: the pilot

    the pilot: 美国电视剧新剧开播都会有一个试播来测试观众对新剧的接受程度,以此来决定是否再继续播下去,也可以说是一个开端,第一集,试播 -Tattoo Artist: That's it. t ...

  10. Linux shell编程札记

    if-then的高级特性 [[]].[]都是用来测试( 测试分为数值比较.字符串比较.文件比价 )的,[[]]是关键字,里面可以用&&.||.<.>等类似C语言的语法:[] ...