题目:

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. linux发展前景如何?

    2014-01-09 18:54Linux将不会取代Windows成为主流的桌面操作环境, 但它很有可能在信息接入设备中独霸天下. 为什么Linux无法取代Windows呢?最主要的原因是大多数最终用 ...

  2. MongoDB Long/Int(长整型)的自增长主键 解决方案

    今朝有幸尝芒果,发现自增长ID类型有多种,唯独没有Long/Int. 一思路:1. 自建一个Collection(表,假设名为:IdentityEntity,其中字段:_id, Key, Value, ...

  3. JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table

    jquery.tmpl.min.js 首先下载这个插件 1.绑定json那头的键 //TemplateDDMX 这个是这段JS的ID,这个必须写!!!!!! //${}为json的键的值,必须要填写正 ...

  4. 转载---linux运维相关

    前段时间,我在准备面试的时搜到的一套Linux运维工程师面试题,感觉比较全面,一直保存在草稿,刚在整理后台时翻了出来,干脆就发出来好了,以备不时之需. 1.linux如何挂在windows下的共享目录 ...

  5. SQLserver通过链接服务器连接oracle

    在SQLserver中一直使用的是DTS抽取数据,但是DTS微软只支持到2008,到了2012后就没有这个工具了,现在需要在SQLserver跟Oracle中间建立一个通道,借助这个通道,将Oracl ...

  6. JavaScript 代码片段

    1.无题 if (i && i.charAt(i.length - 1) == "/") { i = i.substr(0, i.length - 1) } 2.无 ...

  7. iOS 开发之粒子效果

    本文由糖炒小虾.Benna翻译 ,校对:sai.u0u0.iven.子龙山人 iOS 5中的UIKit粒子系统教程 Ray的话:这是第15篇.也是最后一篇<iOS 5 盛宴>中的iOS 5 ...

  8. iTween基础之Look(使对象面朝指定位置)

    一.基础介绍:二.基础属性 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50578142 一.基础介绍 LookTo:旋转游戏对象使其 ...

  9. Jquery-------获取网页参数

    看如下代码: function getURLParameter(name) { return decodeURI( (RegExp(name + '=' + '(.+?)(&|$)').exe ...

  10. Android journey 2 @Android系统框架

    此刻,本人还是一个android菜鸟,刚刚开始起步学习android相关知识,想用blog记录自己学习的过程,一方面给他人提供帮助,另一方面给自己个复习的地方. 在一起学习Android之前,先跟大家 ...