题目:

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. Error:/etc/fstab:Read-only file system错误的解决办法

    1.挂载60T存储,设置开机自动挂载,UUID编号配置错误导致系统无法启动 2.根据提示进入维护状态,输入root密码,进入fstab删除UUID等内容,结果报错     Error:/etc/fst ...

  2. FastLoad错误 — RDBMS error 2634

    我们来看一下下面这条语句: BEGIN LOADING stu_flERRORFILES error_1, error_2;   如果此时已经存在error_1或error_2表,那么将会报错,信息如 ...

  3. Ubuntu下Code::Blocks无法编译 /bin/sh: 1: g++ not found 解决办法

    Linux下Code::Blocks无法编译运行提示 /bin/sh: 1: g++ not found 的解决办法 今天在Ubuntu 12.04 软件中心中选装了Code::Blocks,安装完成 ...

  4. Google protobuf安装

    1:需要安装sudo apt-get install x11-apps libwayland-ltst-client0 libtxc-dxtn-s2tc0 x11-session-utils  x11 ...

  5. Google Code Jam 2015 Round1A 题解

    快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...

  6. NP完全问题

    1.概念 好算法:Edmonds与1975年提出:具有多项式时间(O(nk)的算法为好算法. P类问题:存在多项式时间算法的问题.如:货郎问题.调度问题.最大团问题.最大独立集问题.Steiner树问 ...

  7. win下php5.5.12装不上memcache扩展

    WAMP这个集成环境里,php目录下有个php.ini,apache/bin下也有一个php.ini,环境使用的是apache下的,改apache

  8. java集合类(五)About Map

    接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...

  9. Bootstrap 基础

    一种前端开发框架,如同YUI 下载源码找开后,其文件结构如下: bootstrap/├── css/│   ├── bootstrap.css│   ├── bootstrap.min.css│   ...

  10. HTTP长轮询和短轮询

    http 协议介绍: http 协议是请求/响应范式的, 每一个 http 响应都是由一个对应的 http 请求产生的; http 协议是无状态的, 多个 http 请求之间是没有关系的. http ...