Digit Counts
Count the number of k's between 0 and n. k can be 0 - 9.
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.
* param n : As description.
* return: An integer denote the count of digit k in 1..n
*/
public int digitCounts(int k, int n) {
int totalCount = ;
for (int i = ; i <= n; i++) {
totalCount += counts(k, i);
}
return totalCount;
} public int counts(int k, int value) {
int count = ;
if (value == && k == ) return ;
while (value != ) {
int remainder = value % ;
if (remainder == k) {
count++;
}
value = value / ;
}
return count;
}
};
Digit Counts的更多相关文章
- LintCode "Digit Counts" !!
Lesson learnt: one effective solution for bit\digit counting problems: counting by digit\bit http:// ...
- [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, ...
- 【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 ...
- 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 ...
随机推荐
- CentOS 安装 Jexus
官网:http://www.jexus.org/ 安装过程就照着页面上做就好了,前提是需要安装好mono 在VS2015中新建一个MVC应用程序,这里需要注意两个步骤: 第1步:移除bin下的Micr ...
- hdu3966 树链剖分+成段更新
给你n个点,m条边,p次操作.n个点相连后是一棵树.每次操作可以是x 到 y 增加 z,或者减z,或者问当前点的值是多少. 可以将树分成链,每个点在线段树上都有自己的点,然后线段树成段更新一下. #p ...
- 洛谷P2726 阶乘 Factorials
题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...
- android:sharedUserId 获取系统权限
最近在做的项目,有好大一部分都用到这个权限,修改系统时间啊,调用隐藏方法啊,系统关机重启啊,静默安装升级卸载应用等等,刚开始的时候,直接添加权限,运行就报错,无论模拟器还是真机,在logcat中总会得 ...
- startx启动过程分析
http://blog.csdn.net/hustwarhd/article/details/3069066 JiananHe 09/19/2008 目录 xinit 1.1 功能 1.2 ...
- mysql prepare语句使用
语法 PREPARE statement_name FROM sql_text /*定义*/ EXECUTE statement_name [USING variable [,variable...] ...
- Protocol Buffer技术详解(Java实例)
Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...
- Logistic Regression and Gradient Descent
Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...
- mysql zip 版本配置方法
-\bin 指 C:\Program Files\MySQL\MySQL Server 5.6\bin 1.增加环境变量 "PATH"-"-\bin" 2.修改 ...
- 实时获取UITextField内容
在UISearchBar中,当输入信息改变时,它就会调用textDidChange方法, 但是UITextField没有这个功能,要实现就得手动addTarget,其实controlevent里还有很 ...