233. Number of Digit One
2015-11-28 10:40
375 查看
题目:
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
假定n = 312,我们把这个计算过程分解为几个步骤:
(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)
由此我们可以推出通项公式
Time Complexity - O(log10n), Space Complexity - O(log10n)
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
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
假定n = 312,我们把这个计算过程分解为几个步骤:
(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)
由此我们可以推出通项公式
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
相关文章推荐
- HDU 1238 全枚举
- 【神器】yololib 飘云修改版 -- 给iOS APP 添加导入表注入--你懂的
- 日经春秋 20151128
- 135 Which three statements are true about windows? (Choose three.) A. Only one window can be open at
- 【ios开发】浅谈Target-action模式
- Centos7-mqtt消息中间件mosquitto的安装和配置
- python解决接口测试获取手机验证码问题
- [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server
- 输入命令快速启动应用
- 天声人語 20151128 還暦の自民党に音なし
- Form表单 回车键 防止未校验直接提交
- 侧滑
- SQL SERVER 2008 nvarchar 转换 deciaml 失败(nvarchar to decimal)
- 数据库系统概论----关系运算之除运算
- 自己动手实现一个简单c编译器
- WordPress主题制作(6)-关于页面模板
- Happy Number
- delphi的加密与解密
- 【神器】insert_dylib 给 MacOSX APP 添加导入表注入--你懂的~~
- Java排序算法(十):桶式排序