剑指offer-面试题32-从1到n整数中1出现的次数

来源:互联网 发布:网络设计方案案例 编辑:程序博客网 时间:2024/06/05 19:18

解题思路参考自:http://blog.csdn.net/yi_afly/article/details/52012593

1. 题目描述

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

2. 题目来源

第一次看到是在《剑指Offer》第2版上,面试题32。leetcode和牛客网上都有这道题。

3. 本文的目的

看了《剑指Offer》上的解法,我觉得不能算好:

  1. 这段解释描述有些不清晰,而且没有图,难以理解。
  2. 从书中给出的实现上来看,显得有些凌乱。

在这篇博客里,会给出一个我对这道题的解法,包括完整的解题思路,完整代码,时间复杂度分析,以及在leetcode和牛客网上的提交结果。

4. 解题思路

考虑将n的十进制的每一位单独拿出讨论,每一位的值记为weight。

1) 个位

从1到n,每增加1,weight就会加1,当weight加到9时,再加1又会回到0重新开始。那么weight从0-9的这种周期会出现多少次呢?这取决于n的高位是多少,看图: 



以534为例,在从1增长到n的过程中,534的个位从0-9变化了53次,记为round。每一轮变化中,1在个位出现一次,所以一共出现了53次。 
再来看weight的值。weight为4,大于0,说明第54轮变化是从0-4,1又出现了1次。我们记1出现的次数为count,所以: 
count = round+1 = 53 + 1 = 54

如果此时weight为0(n=530),说明第54轮到0就停止了,那么: 
count = round = 53

2) 十位

对于10位来说,其0-9周期的出现次数与个位的统计方式是相同的,见图: 



不同点在于:从1到n,每增加10,十位的weight才会增加1,所以,一轮0-9周期内,1会出现10次。即rount*10。 

再来看weight的值。

当此时weight为3,大于1,说明第6轮出现了10次1,则: 

count = round*10+10 = 5*10+10 = 60

如果此时weight的值等于0(n=504),说明第6轮到0就停止了,所以: 
count = round*10+10 = 5*10 = 50

如果此时weight的值等于1(n=514),那么第6轮中1出现了多少次呢?很明显,这与个位数的值有关,个位数为k,第6轮中1就出现了k+1次(0-k)。我们记个位数为former,则: 
count = round*10+former +1= 5*10+4 = 55

3) 更高位

更高位的计算方式其实与十位是一致的,不再阐述。

4) 总结

将n的各个位分为两类:个位与其它位。 
对个位来说:

  • 若个位大于0,1出现的次数为round*1+1
  • 若个位等于0,1出现的次数为round*1

对其它位来说,记每一位的权值为base,位值为weight,该位之前的数是former,举例如图: 



则:

  • 若weight为0,则1出现次数为round*base
  • 若weight为1,则1出现次数为round*base+former+1
  • 若weight大于1,则1出现次数为rount*base+base

比如:

  • 534 = (个位1出现次数)+(十位1出现次数)+(百位1出现次数)=(53*1+1)+(5*10+10)+(0*100+100)= 214
  • 530 = (53*1)+(5*10+10)+(0*100+100) = 213
  • 504 = (50*1+1)+(5*10)+(0*100+100) = 201
  • 514 = (51*1+1)+(5*10+4+1)+(0*100+100) = 207
  • 10 = (1*1)+(0*10+0+1) = 2

5. 完整代码

package case32_NumOf1Between1AndN;/** * 题目要求: *  * 输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数 中包含1的数字有1,10,11,12,1 一共出现了5次。 *  * @author WangSai * */public class NumOf1Between1AndN {/** * @param args */public static void main(String[] args) {int n = 999000900;System.out.println("solution1:");long time1 = System.currentTimeMillis();int m = getNumOf1From1ToN_solution2(n);long time2 = System.currentTimeMillis();System.out.println(time2 - time1);System.out.println(m);System.out.println("solution2:");long time3 = System.currentTimeMillis();int p = getNumOf1From1ToN_solution1(n);long time4 = System.currentTimeMillis();System.out.println(time4 - time3);System.out.println(p);}/** * 方法1:从1到n中,统计每个数字中1出现的次数,然后,把所有的次数累加起来。时间复杂度O(nlogn) *  * @param n,最后一个数字n * @return 1出现的总次数 */public static int getNumOf1From1ToN_solution1(int n) {// 异常值检测if (n < 1)return -1;int sum = 0;for (int i = 1; i <= n; i++) {sum += getNumOf1Ofn(i);}return sum;}/** * 统计i中1出现的次数 *  * @param i,待统计的数字 * @return 1在i中出现的次数 */private static int getNumOf1Ofn(int i) {int count = 0;while (i > 0) {if (i % 10 == 1)count++;i /= 10;}return count;}/** * 方法2:根据n分为个位和其他位,[round-weight-former],该方法的时间算法复杂度为O(logn) *  * @param n * @return */public static int getNumOf1From1ToN_solution2(int n) {// 异常值检测if (n < 1)return 0;// 把n分为个位和其他位int round = n;int count = 0;int base = 1;while (round > 0) {// 取余,获取weightint weight = round % 10;// 获取roundround /= 10;// 若weight为0count += round * base;// 判断weight的值,若等于1,则加上former的值,再加1if (weight == 1) {count += n % base + 1;}// 若weight>1,则加上baseelse if (weight > 1) {count += base;}// 基数乘以10,向高位移动一位base *= 10;}return count;}}


阅读全文
0 0
原创粉丝点击