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 ...
随机推荐
- mysql 时间格式与日期格式转换,去除datetime中的具体时间
DATE_FORMAT(`addtime`,'%Y-%m-%d') 时间格式转成字符串 time_format('1924-01-02', '%Y-%m-%d') 字符串转成时间格式 CONVERT ...
- 理解Memcached的分布式
Memcached尽管是"分布式"的缓存系统,但是服务器端并没有分布式功能.各个Memcached实例不会相互通信以共享信息,Memcached如何进行分布式完全取决于客户端的实现 ...
- Myeclipse右键新建项目突然变的很少
额,很是焦躁,最后在界面的右上方点Myeclipse Java enterprise变回原来模样
- iOS: 聊聊 Designated Initializer(指定初始化函数)
iOS: 聊聊 Designated Initializer(指定初始化函数) 一.iOS的对象创建和初始化 iOS 中对象创建是分两步完成: 分配内存 初始化对象的成员变量 我们最熟悉的创建NSOb ...
- Alice and Bob 要用到辗转相减
Alice and BobTime Limit: 1 Sec Memory Limit: 64 MBSubmit: 255 Solved: 43 Description Alice is a be ...
- cocos布局分析
HBox和VBox布局 HBox只是一个水平布局包装类. HBox里面所有的孩子节点都会水平排列成一行 VBox仅仅是对垂直布局的一个简便的类封装. VBox把它的子节点布局在一竖列中. Layout ...
- webservice测试实例
webservice测试实例(LR8.1) 接口声明:这个接口是sina的短信服务接口,我只是用来做脚本学习使用,不会对其产生压力:希望读者也只是用来进行录制学习,而不是产生压力. 接口文档:http ...
- maven之helloworld案例
1.maven目录结构 src -main -java -package -test -java -package -resources 2.新建目录 在任意指定盘下建文件夹(我的是D盘,目录结构如下 ...
- nginx 虚拟主机
基于域名的虚拟主机 创建站点目录 [root@nginx conf]# cd /usr/local/nginx/html/ [root@nginx html]# pwd /usr/local/ngin ...
- C++中的内联成员函数与非内联成员函数
在C++中内联成员函数与非内联成员函数的可以分为两种情况: 1.如果成员函数的声明和定义是在一起的,那么无论有没有写inline这个成员函数都是内联的,如下: using namespace std; ...