题目:

输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,一共出现了5次。

思路:

1、累加法

累加1到n中每个整数1出现的次数。

求每个整数1出现的个数:通过对10求余数,判断整数的个位是否为1,如果商不为0,则继续除以10再判断个位数字是否为1.

时间复杂度:O(nlogn)

2、递归

以21345为例,把1到21345的所有数字分为两段,1-1345,1346-21345。

先看1346-21345,1的出现分为两种情况,1出现在最高位,1出现在其他位。

考虑1出现在最高位:从1346到21345的数字中,1出现在10000-19999这10000个数字的万位中,一共出现10000次(最高位大于1的情况下),当最高位为1时,出现1的次数为除去最高位数字后的数字再加1,如1346-11345,最高位出现1的次数为1345+1=1346次。

考虑1出现在其他位:由于最高位是2,因此1346-21345可以分为两段,1346-11345,11346-21345,每一段剩下的4位中,每一位都可以选择为1,共有4种,而其他三位在0-9之间任意选择,因此根据排列组合原则,总共出现的次数是2*4*10^3=6000.

至于1-1345中1出现的次数,通过上述方法递归得到。这也是为什么要分成1-1345和1346-21345两段的原因,因为把21345的最高位去掉就编成1345,便于采用递归的思路。

总结一下以上的分析结果:

1、1出现在最高位:当最高位为1时,次数等于除去最高位后剩下的数字加1,当最高位大于1时,次数等于10的(位数-1)次方;

2、1出现在其他位:次数等于最高位数字*(总位数-1)*10的(剩下位数-1)次方

时间复杂度:

这种思路每次去掉最高位做递归,递归的次数和位数相同。一个数字有O(logn)位,因此时间复杂度为O(logn).

代码:

1、累加法

#include <iostream>

using namespace std;

int numberOf1(int n){
int count=0;
while(n){
if(n%10==1)
count++;
n=n/10;
}
return count;
} int numberOf1Between1AndN(unsigned int n){
int count=0;
for(unsigned int i=1;i<=n;i++){
count+=numberOf1(i);
}
return count;
} int main()
{
cout << numberOf1Between1AndN(12) << endl;
return 0;
}

2、递归

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h> using namespace std; int PowerBase10(unsigned int n){
int result=1;
for(unsigned int i=1;i<=n;i++)
result*=10;
return result;
} int numberOf1_Recursive(const char* strN){
if(strN==NULL || *strN<'0' || *strN>'9' || *strN=='\0')
return 0;
int first=strN[0]-'0';
unsigned int length=static_cast<unsigned int>(strlen(strN));
if(length==1 && first==0)
return 0;
if(length==1 && first>0)
return 1;
int numFirstDigit=0;
if(first>1)
numFirstDigit=PowerBase10(length-1);
else if(first==1)
numFirstDigit=atoi(strN+1)+1; int numOtherDigit=first*(length-1)*PowerBase10(length-2);
int numRecursive=numberOf1_Recursive(strN+1); return numFirstDigit+numOtherDigit+numRecursive;
} int numberOf1Between1AndN_Recursive(unsigned int n){
if(n<=0)
return 0;
char strN[50];
sprintf(strN,"%d",n);
return numberOf1_Recursive(strN);
} int main()
{
cout << numberOf1Between1AndN_Recursive(12) << endl;
return 0;
}

在线测试OJ:

http://www.nowcoder.com/books/coding-interviews/bd7f978302044eee894445e244c7eee6?rp=2

AC代码:

累加法:

class Solution {
public:
int NumberOf1Between1AndN_Solution(int n)
{
int count=0;
for(int i=1;i<=n;i++)
count+=numberOf1(i);
return count;
} int numberOf1(int n){
int count=0;
while(n){
if(n%10==1)
count++;
n=n/10;
}
return count;
}
};

递归:

class Solution {
public:
int NumberOf1Between1AndN_Solution(int n)
{
if(n<=0)
return 0;
char strN[50];
sprintf(strN,"%d",n);
return numberOf1(strN);
} int numberOf1(const char* strN){
if(strN==NULL || *strN<'0' || *strN>'9' || *strN=='\0')
return 0;
int first=strN[0]-'0';
unsigned int length=static_cast<unsigned int>(strlen(strN)); if(length==1 && first==0)
return 0;
if(length==1 && first>0)
return 1; int numFirstDigit=0;
if(first>1)
numFirstDigit=PowerBase10(length-1);
else if(first==1)
numFirstDigit=atoi(strN+1)+1; int numOtherDigit=first*(length-1)*PowerBase10(length-2);
int numRecursive=numberOf1(strN+1); return numFirstDigit+numOtherDigit+numRecursive;
} int PowerBase10(int n){
int result=1;
for(int i=0;i<n;i++)
result*=10;
return result;
}
};

(剑指Offer)面试题32:从1到n整数中1出现的次数的更多相关文章

  1. 剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)

    问题描述: 输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11,12,1一共出现了5次. 思路:(不考虑时间效率的解法,肯定不 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. phonegap环境搭建

    最近在开发app, html5+php 采用phonegap进行打包 前端框架采用jquery mobile 这里phonegap创建安卓项目 3种方式 1.phonegap 2.cordova 3. ...

  2. adb shell 查找并删除文件

    # -*- coding: cp936 -*- ## function: remove file ## remark: python version -- import os,sys import l ...

  3. 如何正确选择MySQL数据列类型

    MySQL数据列类型选择是在我们设计表的时候经常会遇到的问题,下面就教您如何正确选择MySQL数据列类型,供您参考学习. 选择正确的数据列类型能大大提高数据库的性能和使数据库具有高扩展性.在选择MyS ...

  4. Java文件下载的几种方式

    public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的 ...

  5. nginx服务器防sql注入/溢出攻击/spam及禁User-agents

    本文章给大家介绍一个nginx服务器防sql注入/溢出攻击/spam及禁User-agents实例代码,有需要了解的朋友可进入参考. 在配置文件添加如下字段即可  代码如下 复制代码 server { ...

  6. javascript注意点(1)

    1.void运算符 ECMAScript 262规范,关于void说明如下: The void Operator The production UnaryExpression : void Unary ...

  7. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  8. CSS width:100%和width:auto的区别

    width:100%和width:auto的区别 width:auto比较聪明,如果margin已经左右占去10px的空间,那么width给的值就是580px. <style> div{ ...

  9. Linux_系统信息

    公司里一些仿真软件得进Linux系统,好奇公司用的什么Linux版本,于是搜罗了几个命令如下: 1  uname - Print system info -a, print all info -s, ...

  10. python中基于descriptor的一些概念

    python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...