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 ...
随机推荐
- [转] 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误
原文地址:安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误 最近DotNetCore更新到了1.0.1,Azure tools ...
- [Js/Jquery]立即执行匿名函数
摘要 有时使用js写了一个匿名方法,需要立即执行.因为没有方法名称,无法在其它地方调用. 匿名函数 匿名函数,可以认为是没有方法名称的函数. js中如果想执行匿名函数,结构如下: (function ...
- winsow xp不能安装软件, 提示"中断" 是因为设置了 软件限制策略
原来是我为了优化和安全, 设置了软件限制策略. 我设置的是: secpol.msc中, 设置 "软件限制策略" -> "其他规则"中 , 指定了 c:/d ...
- PHP支付宝接口RSA验证
这两天一直困扰的PHP RSA签名验证问题终于解决了,由于之前RSA接触的不多,再加上官方至今还未有PHP的SDK可供参考,因此走了一些弯路,写在这里和大家分享. 虽然支付宝官方还未提供相关SD ...
- 通过google chrome操作JavaScript中Console
紧接着有关上一个文章的!function................. 前端开发人员一定会用到你的开发者工具中的Console控制台.通常Console用于调试程序,日志输出,打断点等功能.比如我 ...
- 【bzoj1596】[Usaco2008 Jan]电话网络
题目描述 Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无线电通讯塔,来 ...
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- 更新引用google的cdn外部jQuery核心库JS文件
jquery-2.0.3 注!不再支持IE 6/7/8 直接引用地址: <script src="http://ajax.googleapis.com/ajax/libs/jque ...
- gc是什么,什么时候需要gc
Java是由C++发展来的. 它摈弃了C++中一些繁琐容易出错的东西.其中有一条就是这个GC. 写C/C++程序,程序员定义了一个变量,就是在内存中开辟了一段相应的空间来存值.内存再大也是有限的,所以 ...
- HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 解题报告:输入一篇文章,从头开始,当遇到 “Apple”, “iPhone”, “iPod”, ...