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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 【LeetCode】233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  5. 233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  6. 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 ...

  7. 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 ...

  8. 233 Number of Digit One 数字1的个数

    给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...

  9. [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 ...

随机推荐

  1. SwiftyJSON 中文介绍

    SwiftyJSON makes it easy to deal with JSON data in Swift. Why is the typical JSON handling in Swift ...

  2. XSS转码 &amp;&amp; struts2 property标签的bug

    struts2: <s:property value="name" escape="false"/> EL表达式: jsp 2.0中的 ${todo ...

  3. Shell学习:sed命令

    http://blog.sina.com.cn/s/blog_a56ef5490101cn58.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行 ...

  4. Mock框架

    MoQ(基于.net3.5,c#3.0的mock框架)简单介绍 - 你听海是不是在笑 - 博客园 http://www.cnblogs.com/nuaalfm/archive/2009/11/25/1 ...

  5. bzoj2338 数矩形

    给出N(N≤1500)个点,求选四个点作为顶点组成矩形的最大面积,保证有解. 对每两个点连边,按边长排序,枚举等长且中点相同的边作为对角线组成矩形,计算面积取最大值. 时间复杂度O(n2logn) # ...

  6. SqlHelper文件复习

    SqlHelper这是个cs类文件,将经常使用到的数据库操作写到一个文件下,方便调用,以及减少代码量.使用这个文件的前提是要建立一个app.config文件,并且引用configuration程序集: ...

  7. Windows 7无线网卡启用wifi共享蓝屏!

    我的笔记本是联想Y460P,装的是Windows 7 Ultiame(x64)系统,通过设置笔记本的无线(Intel WiFi Link 1000 BGN)搭建Wifi环境并共享,使手机能够通过笔记本 ...

  8. 简单实例讲解linux的module模块编译步骤

    注:原博文地址http://blog.sina.com.cn/s/blog_4ba5b45e0102v25h.html ---------------------------------------- ...

  9. RDA 编译器的搭建

    apt-get install subversion apt-get install make atp-get install gcc sudo vim /etc/profile export PAT ...

  10. PHP注释有意思的排列

    <?php // // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\___ ...