题目:

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.

链接: http://leetcode.com/problems/number-of-digit-one/

题解:

又是数学题,主要思路是用递归来做。还有一些别的解法,二刷的时候争取理解最优解。

下面我们来一步步分析:

  1. n < 1时,结果为0
  2. 1 <= n < 10时,结果为1
  3. 假定n = 312,我们把这个计算过程分解为几个步骤:

    1. (1 ~ 99), 结果为 countDigitOne(99)
    2. (100 ~ 199), 结果为 100 + countDigitOne(99)
    3. (200 ~ 299), 结果为countDigitOne(99)
    4. (300 ~ 312), 结果为countDigitOne(12)
  4. 假定n = 112, 我们也把这个计算过程分解一下:
    1. (1 ~ 99), 结果为 countDigitOne(99)
    2. (100 ~ 112), 结果为 112 - 100 + 1 + countDigitOne(12)
  5. 由此我们可以推出通项公式

Time Complexity  - O(log10n), Space Complexity - O(log10n)

public class Solution {
public int countDigitOne(int n) {
if (n < 1)
return 0;
if (n < 10)
return 1;
int baseInTen = (int)Math.pow(10, String.valueOf(n).length() - 1);
int highestDigit = n / baseInTen; // get the highest digit of n if(highestDigit == 1)
return countDigitOne(baseInTen - 1) + (n - baseInTen + 1) + countDigitOne(n % baseInTen);
else
return highestDigit * countDigitOne(baseInTen - 1) + baseInTen + countDigitOne(n % baseInTen);
}
}

Reference:

https://leetcode.com/discuss/44281/4-lines-o-log-n-c-java-python

https://leetcode.com/discuss/44279/clean-c-code-of-log10-complexity-with-detailed-explanation

https://leetcode.com/discuss/44314/accepted-solution-using-counting-principle-with-explanation

https://leetcode.com/discuss/44465/my-ac-java-solution-with-explanation

https://leetcode.com/discuss/44617/my-recursion-implementation

https://leetcode.com/discuss/47774/0ms-recursive-solution-in-c-8-line-code

https://leetcode.com/discuss/46366/ac-short-java-solution

https://leetcode.com/discuss/64604/my-simple-and-understandable-java-solution

https://leetcode.com/discuss/64962/java-python-one-pass-solution-easy-to-understand

https://leetcode.com/discuss/54107/0-ms-recursive-solution

https://leetcode.com/discuss/58868/easy-understand-java-solution-with-detailed-explaination

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

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

  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(统计1出现的次数)

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

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

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

  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. Oracle物理的体系结构

    体系结构图的学习: 老余服装店的故事 结构图: SQL查询语句 SGA 共享池shared pool 数据缓存区Buffer cache PGA 进程 SQL更新语句 SGA: 日志缓存区 日志文件 ...

  2. dblink 的源数据表结构修改后在存储过程里执行报错

    原情况:A服务器表A服务器B也有一张表A服务器B上有一个存储过程要把本地的head表向A服务器表里插入数据.变更后:在A服务器表里增加了一个字段inserttime,服务器B存储过程本地表向A服务器插 ...

  3. linux内核中的get_user和put_user

    linux内核中的get_user和put_user 在 内核空间和用户空间交换数据时,get_user和put_user是两个两用的函数.相对于copy_to_user和 copy_from_use ...

  4. iOS多线程GCD 研究

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  5. (菜鸟要飞系列)四,基于Asp.Net MVC5的后台管理系统(zTree绑定Json数据生成树)

    上一次老师让我们用递归将中国城市镇县四级联动 显示在树上,那个时候就知道可以显示在zTree上,可是苦于对Json的不了解,对zTree的Api的不了解,一直没有做出来,只好将递归算法显示在了窗体上, ...

  6. String对象中常用的方法

    String对象中常用的方法   1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...

  7. 清除SQL Server执行计划

    有时需要调试SQL语句的性能, 需要不断的执行SQL语句, 可是多次执行同一条语句的时候,SQL Server 会缓存表的数据,结果就测不出来 实际的 SQL 的性能 用以下SQL可以清除缓存数据 D ...

  8. springMvc基本注解:@Component、@Repository(持久层) 、@Service(业务逻辑) 、@Controller(控制层)

    1.@Controller(控制层) :就是action层 2.@Service(业务逻辑) :业务逻辑层,负责处理各种控制层的操作 3.@Repository(持久层) :称为“持久化”层,负责对数 ...

  9. C# XML - XmlNode and XmlAttribute

    public static string TestXML(string path) { XmlDocument doc = new XmlDocument(); doc.Load(path); Xml ...

  10. 3563: DZY Loves Chinese - BZOJ

    Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...