剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)
问题描述:
输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1的数字有1,10,11,12,1一共出现了5次。
思路:(不考虑时间效率的解法,肯定不是面试官期望的)
直观想法:累加1到n中每个整数中1出现的次数。
每个整数中1出现的次数可以由除以10和模10来计算得到。
代码如下:
boolean invalidInput = false;
//不考虑时间效率的解法
public int NumberOf1Between1AndN_Solution(int n) {
if(n <= 0){
invalidInput = true;
return 0;
}
int number = 0;
for(int i = 1; i <= n; i++){
number += NumberOf1(i);
}
return number;
}
public int NumberOf1(int n){
int number = 0;
while(n > 0){
if(n % 10 == 1){
number++;
}
n = n/10;
}
return number;
}
思路2:从数字规律着手明显提高时间效率的解法,能让面试官耳目一新
首先将1-n根据最高为分成两段,比如1-21345,分为1-1345与1346-21345
从最高位开始,每次分析最高位出现1的次数(用字符串)。然后剥去最高位进行递归求解。
代码:
public class Solution {
boolean invalidInput = false;
//时间复杂度为O(logn)
public int NumberOf1Between1AndN_Solution(int n) {
if(n <= 0){
invalidInput = true;
return 0;
}
StringBuilder s = new StringBuilder(((Integer)n).toString());
return NumberOf1(s);
}
int NumberOf1(StringBuilder s){
if(s == null || s.length() == 0 || s.charAt(0) < '0' || s.charAt(s.length()- 1) > '9'){
return 0;
}
int first = s.charAt(0) - '0';
int length = s.length();
if(length == 1 && first == 0){
return 0;
}
if(length == 1 && first > 0){
return 1;
}
//假设n = 21345
//numFirstDigit是数字10000 - 19999 的第一个位中的数目
int numFirstDigit = 0;
if(first > 1){
numFirstDigit = PowerBase10(length - 1);
}else if(first == 1){
numFirstDigit = Integer.parseInt(s.substring(1)) + 1;
}
//numOtherDigits是1346 - 21345除了第一位之外的数位中的数目
int numOtherDigits = first * (length - 1) * PowerBase10(length - 2);
//numRecursive是1 - 1345中的数目
int numRecursive = NumberOf1(s.deleteCharAt(0));
return numFirstDigit + numOtherDigits + numRecursive;
}
int PowerBase10(int n){
int result = 1;
for(int i = 0; i < n; i++){
result *= 10;
}
return result;
}
}
剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)的更多相关文章
- 【剑指Offer面试编程题】题目1373:整数中1出现的次数--九度OJ
题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他.问题是:求出1~13的整数中1出现的次数,并算出100~130 ...
- 剑指Offer的学习笔记(C#篇)-- 整数中1出现的次数(从1到n整数中1出现的次数)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- 【剑指Offer】31、从1到n整数中1出现的次数
题目描述: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他 ...
- 【剑指Offer面试题】 九度OJ1517:链表中倒数第k个结点
鲁棒性是指程序可以推断输入是否符合规范要求,并对不和要求的输入予以 合理的处理. 题目链接地址: http://ac.jobdu.com/problem.php?pid=1517 题目1517:链表中 ...
- 剑指offer——面试题32.1:分行从上到下打印二叉树
void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode ...
- 剑指offer——面试题32:从上到下打印二叉树
void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...
- 【剑指offer 面试题38】数字在排序数组中出现的次数
思路: 利用二分查找,分别查找待统计数字的头和尾的下标,最后做差加一即为结果. C++: #include <iostream> #include <vector> using ...
- 剑指offer——面试题15.2:判断两个整数m和n的二进制中相差多少位
#include"iostream" using namespace std; int CountDifferentBit(int m,int n) { ,diff=m^n; wh ...
- 面试题32.从1到n整数中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...
随机推荐
- linux创建静态库
[1]新建源程序staticlib.c /************************************************************************* > ...
- mysql之替换字符串
update `wp_posts` set `post_content`=REPLACE(`post_content`,'localhost/linkcp','www.linkcp.cn') wher ...
- HTTP状态代码含义
1.200:正常. 2.201:服务器已经创建了文档,Location头给出了它的URL. 3.202:已经接受请求,但处理尚未完成. 4.203:文档已经正常返回,但一些应答头可能不正确,因为使用的 ...
- Chapter 4: Troubleshoot and debug web applications
Prevent and troubleshoot runtime issues Troubleshooting performance, security and errors using perfo ...
- wordmate 词典安装
wordmate,使用 StarDict 的词典,可称为 Android 上的 StarDict 安装 wordmate 后,会在 SD 卡中生成 wordmate 文件夹,词典便放在此目录 下载词典 ...
- 【python】调用机器喇叭发出蜂鸣声(Beep)
##coding:utf-8 import winsound winsound.Beep(600,6000) #其中600表示声音大小,1000表示发生时长,1000为1秒
- jquery 让滚动条处于div底部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【Jersey】基于Jersey构建Restful Web应用
环境说明 java: 1.6: tomcat: 6.0.48: Jersey:1.18: Jersey介绍 主要用于构建基于Restful的Web程序: 构建基于Maven的Javaweb程序 说明: ...
- Configure PPPoE on CentOS7
Why? I prefer using ethernet to Wifi to access internet. But, I'm afraid, sometimes I have to use Wi ...
- 资金归集率比率sql
基础资料 select bd_glorgbook.glorgbookcode, nvl(replace(bd_glorgbook.glorgbookname,'集团基准账薄',''),'小计')公司名 ...