(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 ...
随机推荐
- Spring security与shiro
shiro更轻量级,spring security过于复杂. Apache Shiro 使用手册(一)Shiro架构介绍 Spring Security笔记:Remember Me(下次自动登录)
- Shell学习:sed命令
http://blog.sina.com.cn/s/blog_a56ef5490101cn58.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行 ...
- CGI相关概念
common gateway interface 通用网关接口 可以让客户端从浏览器向执行在服务器上的程序请求数据.CGI描述了客户端和服务器程序之间传输数据的一种标准. 编程语言perl是一种被广泛 ...
- bzoj1006 神奇的国度
Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系 ...
- Puppet master/agent installation on RHEL7
==================================================================================================== ...
- 求助:为什么我用360浏览器和UC浏览器打不开JAVA中的index.html文件? 一打开就显示浏览器首界页
如下图,在oracle官网下载了一个JAVA的API文档,双击index.html时打开是浏览器的首页,不知道为什么?请问怎样才能以chm文档格式显示?
- linux 鼠标中键粘帖功能?!!
转载自:http://yjhexy.iteye.com/blog/785564 ubuntu鼠标中键问题,其实也不是什么问题,ubuntu的鼠标中键是用来快速粘贴的,只是windows用惯了,时不时手 ...
- (一)java的由来
java的诞生:每一次设计语言的革新都是为了解决先前语言所遇到的不能解决的问题,B语言导致C语言的诞生,C语言演变成C++,java则继承了这两种语言的大部分特性.java最初的推动力是为了找到一种能 ...
- Orcale(一)概念
一 . 基本概念 : 1. orcaleinstanceclient : orcale的客户端 ( 1. 中文乱码问题 : NLS_LANG=SIMPLIFIED CHINESE_CHINA.Z ...
- PLSQL_性能优化系列08_Oracle Insert / Direct Insert性能优化
2014-09-25 Created By BaoXinjian