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 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.
解题思路:
递归
static public int countDigitOne(int n) {
if (n == 0)
return 0;
if (n < 10)
return 1;
int length = 0;
int firstNum = n;
while (firstNum >= 10) {
firstNum /= 10;
length++;
}
int basic = (int) Math.pow(10, length);
if (firstNum > 1) {
int tmp1 = countDigitOne(basic - 1);
int tmp2 = basic;
int tmp3 = countDigitOne(n - basic * firstNum);
return firstNum * tmp1 + tmp2 + tmp3;
} else {
int tmp1 = countDigitOne(basic - 1);
int tmp2 = n + 1 - basic;
int tmp3 = countDigitOne(n - basic);
return tmp1 + tmp2 + tmp3;
} }
Java for LeetCode 233 Number of Digit One的更多相关文章
- (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 某一范围内的整数包含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 ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 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 *HARD* -- 从1到n的整数中数字1出现的次数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 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 ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- android自定义控件(2)-拖拽实现开关切换
在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...
- [css]通过transform缩放邮件客户端h5页面
摘要 最近一直在折腾邮件通知的东东,大概逻辑就是如果有新邮件,向收件人的app推送一条服务号消息,并且在单击该消息的时候,需要展示邮件的详情. 技术 这里是使用Exchange EWS API来实现的 ...
- C语言之strrchr函数
from:http://blog.csdn.net/hgj125073/article/details/8443912 [FROM MSDN && 百科] 原型:char *strrc ...
- css外边距合并和z-index的问题
参考这篇文章, 将外边距的 折叠 参考这篇文章, 将bfc的生成, bfc的应用 参考这篇文章 position: absolute的元素, 仍然具有内填充padding和border边框属性样式, ...
- AngularJS API之toJson 对象转为JSON
toJson()能把对象序列化为json 方法讲解 这个方法最多支持2个参数: angular.toJson(obj, pretty); obj 是想要转换的对象, pretty 可以调节格式化的样式 ...
- 湖南附中模拟day1 收银员
4.1 题意描述花花家的超市是 24 小时营业的,现在需要招聘收银员.超市每个小时都需要不同数量的收银员,用 ai 表示一天中 i 点到 i + 1 点这一小时内需要的收银员数量,特别地 a23 表示 ...
- select2
.select2-container .select2-choice { height: 34px; line-height: 34px; } .自定义 组件高度 在css 里面设置 .select2 ...
- java 练手 Fibonacci数
Problem B Fibonacci数 时间限制:3000 ms | 内存限制:65535 KB 描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...
- bootstrap-tab
功能:点击时切换相应的内容或图片 插件:tab.js 要点:tab标签用在导航条上,以data-toggle作被点击者, 以tab-content作内容显示 <!DOCTYPE html> ...
- APPCAN IDE中安装emmet插件
1.首先打开APPCAN IDE 2.帮助(help)-安装新软件(install New sofaWare) 3.打开Install窗口,点击 Add,在Add Repository窗口中,Name ...