Digit Counts
2024-10-13 02:47:21
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, 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;
}
};
最新文章
- source /etc/profile报错-bash: id:command is not found
- hibernate的一种报错
- 刚看到的感觉会用的到 收藏一下 常用的iOS第三方资源 (转)
- 关于SCRUM站立会议
- js实现对数据库的增删查改
- 使用GDI+轻松创建缩略图
- EasyUI的下拉选择框控件方法被屏蔽处理方式
- 插件式Web框架
- Chrome调试大全
- 【Spring 核心】AOP 面向切面编程
- springBoot系列教程03:redis的集成及使用
- nodejs http小爬虫
- 初识 go 语言
- advanceskeleton插件分身体和表情单独绑定的时候合并表情步骤
- NAT详解:基本原理、穿越技术(P2P打洞)、端口老化等
- ssh登陆报错:packet_write_wait: Connection to x.x.x.x port 22: Broken pipe
- 检查mono兼容性的工具MOAM
- php中正则表达式的语法规则
- 廖雪峰老师Python教程读后笔记
- springboot整合thumbnailator实现图片压缩
热门文章
- struts的上传和下载
- 利用css3实现超出文本指定行数与省略号效果
- 【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor
- Java编程思想学习(二) 操作符
- Linux Process/Thread Creation、Linux Process Principle、sys_fork、sys_execve、glibc fork/execve api sourcecode
- RIP、OSPF、BGP、动态路由选路协议、自治域AS
- OracleOraDb10g_home1TNSListener无法启动
- 使用guava带来的方便
- GTP V0 和 GTP V1
- 基础知识系列☞Abstract和Virtual→及相关知识