233. Number of Digit One
题目:
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/
题解:
又是数学题,主要思路是用递归来做。还有一些别的解法,二刷的时候争取理解最优解。
下面我们来一步步分析:
- n < 1时,结果为0
- 1 <= n < 10时,结果为1
- (1 ~ 99), 结果为 countDigitOne(99)
- (100 ~ 199), 结果为 100 + countDigitOne(99)
- (200 ~ 299), 结果为countDigitOne(99)
- (300 ~ 312), 结果为countDigitOne(12)
- 假定n = 112, 我们也把这个计算过程分解一下:
- (1 ~ 99), 结果为 countDigitOne(99)
- (100 ~ 112), 结果为 112 - 100 + 1 + countDigitOne(12)
- 由此我们可以推出通项公式
假定n = 312,我们把这个计算过程分解为几个步骤:
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的更多相关文章
- 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 ...
- 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 ...
- (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 ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 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 ...
- 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 ...
- 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 ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- [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 ...
随机推荐
- MySQL多实例-精典故障案例
很久以前搭建过MySQL多实例,记得当时很顺利,呵呵!今天公司因为业务需要,我再一次搭建多实例.安装完MySQL后,初始化两个实例时,出现如下报错: 150915 1:10:36 [ERROR] C ...
- XHTML1.0对HTML4.0的改进
1.XHTML借鉴了XML的写法,语法更加严格: 2.XHTML实现了把页面样式和内容分离了,废弃了HTML4.0中表示样式的标签和属性,推荐使用CSS样式来描述页面的样式. XHTML1.0 分为两 ...
- 记一次Surface Pro 2还原操作
因为要做Azure的一个case,对自己的域环境下直接进行了捕获.结果导致机器直接crash. 重启后使用本地账号登陆后发现所有Win 8 的App都无法使用,包括进入设置中还原方式也无法使用. 可以 ...
- Hive基本命令整理
创建表: hive> CREATE TABLE pokes (foo INT, bar STRING); Creates a table called pokes with t ...
- oracle-12c-rac 报:ORA-01078
OS: Oracle Linux Server release 5.7 DB: Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - ...
- Linux关机命令总结
在linux命令中reboot是重新启动,shutdown -r now是立即停止然后重新启动,都说他们两个是一样的,其实是有一定的区别的. shutdown命令可以安全地关闭或重启Linux系统,它 ...
- 加载驱动模块时Device or resource busy的解决方法
加载驱动模块时Device or resource busy的解决方法 加载驱动模块时Device or resource busy的解决方法 insmod或modprobe驱动模块时Device o ...
- 从零开始学ios开发(十二):Table Views(上)
这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...
- yii2怎样写规则可以隐藏url地址里的控制器名字
yii2怎样写规则可以隐藏url地址里的控制器名字,例如现在的是***.com/site/index.html要变成***.com/index.html '<action:index>.h ...
- python学习小结4:类
虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 类和对象是面向对象编程的两个主要方面.类:创建一个新类型,而对象是这个类的实例,类使用class关键字创建.类的域和方法被列在一个 ...