(medium)LeetCode 233.Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
解法:参考编程之美 132页 2.4 1的数目,以下代码中,注意iFactor有可能超越int所能表示的范围,故将其类型定义为long,为了避免过多的强制类型转换,其他变量也定义为long,但是iCurNum除外,switch()中变量不能为long类型。(以下以百位数1的个数为例)
百位数为0:百位数上可能出现的1的次数由高位决定,=更高位数*当前权值(100);受高位影响
百位数为1:=低位数字+1 +百位数为0的情况;受高位和低位影响
百位数大于1:(高位数+1)*当前权值(100);受高位影响
代码如下:
public class Solution {
public int countDigitOne(int n) {
if(n<=0) return 0;
long iCount=0;
long iFactor=1;
long iLowerNum=0;
int iCurrNum=0;
long iHigherNum=0;
while(n/iFactor!=0){
iLowerNum=n-(n/iFactor)*iFactor;
iCurrNum=(int)(n/iFactor)%10;
iHigherNum=n/(iFactor*10);
switch(iCurrNum){
case 0:
iCount=iCount+iHigherNum*iFactor;
break;
case 1:
iCount+=iHigherNum*iFactor+iLowerNum+1;
break;
default:
iCount+=(iHigherNum+1)*iFactor;
break;
}
iFactor*=10;
}
return (int)iCount;
}
}
运行结果:
(medium)LeetCode 233.Number of Digit One的更多相关文章
- Java for LeetCode 233 Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- leetcode 233 Number of Digit One
这题属于需要找规律的题.先想一下最简单的情形:N = 10^n - 1 记X[i]表示从1到10^i - 1中 1 的个数,则有如下递推公式:X[i] = 10 * X[i - 1] + 10^(i ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 233. Number of Digit One(统计1出现的次数)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
随机推荐
- windows下做react native官方例子遇到的问题
1.android/app/build.gradle文件中,指定了版本: compileSdkVersion 23buildToolsVersion "23.0.1" 需要在设置中 ...
- 使用druid连接池的超时回收机制排查连接泄露问题
在工程中使用了druid连接池,运行一段时间后系统出现异常: Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: ...
- 反向代理代理百度、google
<VirtualHost _default_:443> # ServerAdmin mail@localhost # DocumentRoot "/var/www/html&qu ...
- kettle 数据库连接中断重置
项目适用kettle作为etl工具,源数据库为mysql库,目标库为oracle.在持续的循环调度中,经常发现oracle的数据库连接中断,需要重置. 具体报错信息如下: INFO 26-12 23 ...
- 详解MySQL大表优化方案( 转)
当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...
- LINUX常用配置及命令
一. Fedora系统配置 1. [设置网卡IP] 步骤如下: 1) 用root用户登陆,打开/etc/sysconfig/network-scripts/ifcfg-eth0文 ...
- mybatis——动态sql
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- 【mybaits】Mybatis中模糊查询的各种写法
工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{t ...
- 错误:javax.servlet.jsp.PageContext can not be to a type
在写Jsp文件时,引入script源文件(<script type="text/javascript" src="${pageContext.request.con ...
- 剑指offer系列60---第一个只出现一次的字符
[题目]在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置. * 若为空串,返回-1.位置索引从0开始 * [思路]1 首先遍历字符串数组,添 ...