题目:

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. php进行多个数组合并zip

    $a = array_zip(array("Dog","Cat","Horse"), array(1,2,3), array("l ...

  2. How to force to Fullscreen Form

    Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...

  3. MySQL数据库主从复制

    一.MySQ主从复制(主库写入数据,从库读取数据) MySql官方下载地址:http://dev.mysql.com/downloads/mysql/ MySql常用命令: 设置密码 UPDATE U ...

  4. js中的数组

    上网查了一下,js中的数组包含的内容还真不少.先给出两个学习的链接: w3school链接:http://www.w3school.com.cn/js/js_obj_array.asp 博客园链接:h ...

  5. 在Java中执行js代码

    在某些特定场景下,我们需要用Java来执行Js代码(如模拟登录时,密码被JS加密了的情况),操作如下: ScriptEngineManager mgr = new ScriptEngineManage ...

  6. Java从入门到精通——基础篇之JSTL标签

    一.语言基础 EL(Expression Language)表达式,目的:为了使JSP写起来更加简单.提供了在 JSP 中简化表达式的方法. 二.分类 核心标签库:提供条件判断.属性访问.URL处理及 ...

  7. CrossDomain.xml的作用及其简单用法

    使用crossdomain.xml让Flash可以跨域传输数据 本文来自http://www.mzwu.com/article.asp?id=975 一.概述 位于www.mzwu.com域中的SWF ...

  8. 转:一份基础的嵌入式Linux工程师笔试题

    一. 填空题: 1. 一些Linux命令,显示文件,拷贝,删除 Ls cp rm 2. do……while和while……do有什么区别? 3. Linux系统下.ko文件是什么文件?.so文件是什么 ...

  9. 在IOS中使用json

    1.从https://github.com/stig/json-framework/中下载json框架:json-framework 2.解压下载的包,将class文件夹下的所有文件导入到当前工程下. ...

  10. RCP,TCP,C/S,B/S

    RCP: RICH CLIENT PROGRAM   胖客户端 TCP: THIN CLIENT PROGRAM   瘦客户端 CS: CLIENT SERVER             客户端/服务 ...