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. 【转】C++11中的std::function

    原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...

  2. hexo git配置问题笔记

    本地安装hexo步骤 注意:本节教程只针对Windows用户,Linux和Mac用户请移步hexo安装. 安装Git 下载 msysgit 并执行即可完成安装. 安装Node.js 在 Windows ...

  3. Java-->实现断点续传(下载)

    --> 断点续传: 就像迅雷下载文件一样,停止下载或关闭程序,下次下载时是从上次下载的地方开始继续进行,而不是重头开始... --> RandomAccessFile --> poi ...

  4. ajax 轮循

    使用 AJAX 进行异步加载轮询操作.简单代码如下: <script> // 执行ajax轮循操作 function polling(){ var xmlhttp; // 判断浏览器--创 ...

  5. ZOJ 1029 Moving Tables

    原题链接 题目大意:走廊两边排列了400个房间,要在两个房间之间搬桌子.搬桌子的时候会占用一部分走廊,有冲突的话要回避.求最快搬完的时间. 解法:开辟一个数组,每占用一段走廊,就把相应的房间号的元素加 ...

  6. android之官方下拉刷新组件SwipeRefreshLayout

    1.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器. 2.setProgressBackgr ...

  7. SQL Server 合并表 union 和union all

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...

  8. Vue.js相关知识4-路由

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. PHP迭代

    计算1到10的和: <?php function f($n) { $before_1 = 1; $before_2 = 1; for( $i = 3;$i <= $n;++$i ) { $ ...

  10. PS CS5

    1.图层 背景图层:双击解锁 右下按键:新建图层.删除图层.新建图层组 眼睛-图层的显示与隐藏 缩略图大小选择:右上三角-面板设置 打开那个图片然后选择工具里面的移动工具然后按住鼠标拖动,拖到那个图片 ...