问题描述:

输入一个整数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实现)的更多相关文章

  1. 【剑指Offer面试编程题】题目1373:整数中1出现的次数--九度OJ

    题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他.问题是:求出1~13的整数中1出现的次数,并算出100~130 ...

  2. 剑指Offer的学习笔记(C#篇)-- 整数中1出现的次数(从1到n整数中1出现的次数)

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  3. 【剑指Offer】31、从1到n整数中1出现的次数

      题目描述:   求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他 ...

  4. 【剑指Offer面试题】 九度OJ1517:链表中倒数第k个结点

    鲁棒性是指程序可以推断输入是否符合规范要求,并对不和要求的输入予以 合理的处理. 题目链接地址: http://ac.jobdu.com/problem.php?pid=1517 题目1517:链表中 ...

  5. 剑指offer——面试题32.1:分行从上到下打印二叉树

    void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode ...

  6. 剑指offer——面试题32:从上到下打印二叉树

    void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...

  7. 【剑指offer 面试题38】数字在排序数组中出现的次数

    思路: 利用二分查找,分别查找待统计数字的头和尾的下标,最后做差加一即为结果. C++: #include <iostream> #include <vector> using ...

  8. 剑指offer——面试题15.2:判断两个整数m和n的二进制中相差多少位

    #include"iostream" using namespace std; int CountDifferentBit(int m,int n) { ,diff=m^n; wh ...

  9. 面试题32.从1到n整数中1出现的次数

    题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...

随机推荐

  1. c语言数据结构之 快速排序

    编译器:VS2013 #include "stdafx.h"#include<stdlib.h> //函数声明 void QuickSort(int a[],int n ...

  2. linux的压缩命令

    gzip: Linux压缩保留源文件的方法: gzip –c filename > filename.gz Linux解压缩保留源文件的方法: gunzip –c filename.gz > ...

  3. {part1}DFN+LOW(tarjan)割点

    什么是jarjan? 1)求割点 定义:在无向连通图中,如果去掉一个点/边,剩下的点之间不连通,那么这个点/边就被称为割点/边(或割顶/桥). 意义:由于割点和割边涉及到图的连通性,所以快速地求出割点 ...

  4. 将网页另存为PDF文件的方法

    使用google chrome浏览器测试,其他浏览器应该也是差不多的方法. 步骤1: 打开需要转换的网页: 步骤2: 点击右上角的三点按键,或者快捷键Ctrl+P,调用的打印页面: 步骤3: 选择目标 ...

  5. python字符串基础知识

    1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...

  6. 《Matrix Computation 3rd》读书笔记——第4章 特殊线性系统

  7. (转)The Neural Network Zoo

    转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...

  8. SQL Server 快捷键备忘

    Run the sp_help system stored procedure ALT+F1 Run the sp_who system stored procedure CTRL+1 Run the ...

  9. OSX unable to write 'random state'

    openssl ca -gencrl -config ./config/openssl.cnf -out ./CA/crl/cacrl.pem -passin pass:'password' unab ...

  10. NetflixOSS:Hollow正式发布

      http://www.infoq.com/cn/articles/netflixoss-hollow-officially-released "如果你能 非常有效地 缓存 一切 ,那么通 ...