LintCode "Digit Counts" !!
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" !!的更多相关文章
- 【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 ...
- 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, ...
- [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, ...
- 欧拉工程第63题:Powerful digit counts
题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...
- 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 ...
- 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, ...
- Project Euler 63: Powerful digit counts
五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 剑指offer ------ 刷题总结
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...
随机推荐
- 【转】eclipse 安装插件
eclipse安装插件或许没有什么多的可讲,但对于刚刚接触eclipse这款IDE的新手来说,可能还是会有点棘手! eclipse安装插件大致三种方法: 方法一:自身安装器 使用eclipse的Sof ...
- How to use HaploView
HaploView is a program that is used to visualize the LD blocks of SNPs. What I need to do is the fo ...
- live555源代码编译
参考http://www.cnblogs.com/MikeZhang/archive/2013/04/24/live555Windows_20130424.html 环境:windowsxp + VS ...
- Apache配置站点根目录、用户目录及页面访问属性
一.配置站点根目录及页面访问属性 DocumentRoot "/www/htdoc" <Directory "/www/htdoc"> Option ...
- 微信JS-SDK应用DEMO
首先需要将以下函数写入TinkPHP的公用function.php文件中以便调用 // 基于ThinkPHP /** * php curl 请求链接 * 当$post_data为空时使用GET方式发送 ...
- meta name="viewport" 属性详解
随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而,每一款手机有不同的分辨率,不同屏幕 ...
- jsp获取SessionID值
<% HttpSession s = request.getSession(); s.setAttribute("name","test"); %> ...
- Memory Barriers ,cache-coherency
http://www.rdrop.com/users/paulmck/scalability/paper/whymb.2010.07.23a.pdf Shared-Memory Synchroniza ...
- GPU的革命
CUDA 线程执行模型分析(一)招兵------ GPU的革命 CUDA 线程执行模型分析(二)大军未动粮草先行------GPU的革命 CUDA硬件实现分析(一)------安营扎寨-----GPU ...
- HBase 建表新增数据记录
login as: root root@192.168.12.23's password: ********* Last login: Wed Aug 20 00:41:17 2014 from 19 ...