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 假定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 nif(highestDigit == 1)return countDigitOne(baseInTen - 1) + (n - baseInTen + 1) + countDigitOne(n % baseInTen);elsereturn 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相关推荐
- 233. Number of Digit One 详细解答
Approach #1 Brute force [Time Limit Exceeded] Intuition Do as directed in question. Algorithm Iterat ...
- leetcode 233 number of digit one
假设 n = xyzdabc,此时我们求千位是 1 的个数,也就是 d 所在的位置. 那么此时有三种情况, d == 0,那么千位上 1 的个数就是 xyz * 1000 d == 1,那么千位上 1 ...
- LeetCodeOJ.Number of Digit One
试题请参见: https://leetcode.com/problems/number-of-digit-one/ 题目概述 Given an integer n, count the total n ...
- [LeetCode]Number of Digit One,解题报告
题目 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less ...
- LeetCode Number of Digit One(计算1的个数)
题意:给出一个整数n,求从0到n之间的数中出现1的总个数 思路: 根据 high cur low(其中cur是1位),而number表示cur当前的位数(1,10,100,...) 当cur=0时 ...
- LeetCode Number of Digit One
原题链接在这里:https://leetcode.com/problems/number-of-digit-one/ 每10个数, 有一个个位是1, 每100个数, 有10个十位是1, 每1000个数 ...
- 【JS】Number to digit tiers
创建一个接受数字的函数,并返回一个字符串数组,其中包含在每个数字处截取的数字. 示例 420 结果为 ["4", "42", "420"] ...
- 《剑指offer》全解(图文结合,通俗易懂,一篇看爽)
3. 数组中重复的数字 题目链接 牛客网 题目描述 在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次.请 ...
- LeetCode github集合,附CMU大神整理笔记
Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...
最新文章
- 今晚8点直播 | 详解聊天机器人落地及进阶实战
- shufflenet-centernet
- pythonloop循环结构_python - tensorflow,tf.while_loop:这两个结构没有相同的嵌套结构 - SO中文参考 - www.soinside.com...
- 如何打开手机端口_微信接收图纸dwg怎么打开?如何手机查看CAD图纸,三步免费教你...
- EXCEL 列与列怎么交换?
- 数据结构---Kruskal最小生成树
- mysql优化 运维_MySQL运维---MySQL优化
- DataGridView中的CheckBox
- 逻辑回归与线性回归的区别
- 游戏设计要素探秘之术语的呼唤
- matlab filter函数_简单使用matlab滤波器
- python实现三级菜单
- Docker 安装 zookeeper
- java正则表达式yyyymmdd_java验证日期yyyyMMdd正则表达式,
- Nginx配置https及证书
- android剪贴板历史,可能是史上最便捷的剪贴板应用 -- Native Clipboard #Android
- 硅谷录用的计算机专业大学排名,学计算机科学专业,必选硅谷附近的这些加州大学...
- Java job interview:项目架构研发京东大数据价值最大化的应用实践
- yolov5模型转换(pt=>onnx=>rknn)和板端验证测试
- check if DVE variable is valid