面试题 32: 从1到n 整数中1出现的次数
来源:互联网 发布:mac终端打开文件 编辑:程序博客网 时间:2024/05/24 03:04
一. 题目
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次.
代码请到我的代码库中下载 Point2Offer
二. 代码
package week_3;/**难度系数:***** * 剑指offer: 从1到n整数中1出现的次数 * 方法: 数字规律 * 测试用例:功能测试(0,1(边界值),5,99,10000(大数)) * @author dingding * Date:2017-6-28 13:25 * Declaration: All Rights Reserved! */public class No32 { public static void main(String[] args) { test1(); test2(); test3(); test4(); test5(); test6(); } //solution private static int findAllOf1From1ToN(int n){ if (n<=0) { return 0; } String value = n+""; //整数转为字符串 int[] numbers = new int[value.length()]; for (int i=0;i<numbers.length;i++){ numbers[i] = value.charAt(i)-'0'; //字符转数字 } return numberOf1(numbers,0); } //统计1出现次数,递归 private static int numberOf1(int[] numbers, int curIndex) { if (numbers == null || curIndex>=numbers.length||curIndex<0) { return 0; } int first = numbers[curIndex]; // 待处理的第一个数字 int length = numbers.length - curIndex; //所在位数 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(numbers,curIndex+1)+1; } int numOtherDigits = first * (length-1) * powerBase10(length-2); int numRecursive = numberOf1(numbers, curIndex+1); return numFirstDigit+numOtherDigits+numRecursive; } //数字数组转换为数值 private static int atoi(int[] numbers, int i) { int result = 0; for (int j=i;j<numbers.length;j++){ result = (result*10+numbers[j]); } return result; } //求10的n次方 private static int powerBase10(int n) { int result = 1; for (int i=0;i<n;i++){ result *=10; } return result; } /*=====================测试用例======================*/ private static void test1() { System.out.println(findAllOf1From1ToN(1)); // 1 } private static void test2() { System.out.println(findAllOf1From1ToN(0)); // 0 } private static void test3() { System.out.println(findAllOf1From1ToN(10)); // 2 } private static void test4() { System.out.println(findAllOf1From1ToN(55)); } private static void test5() { System.out.println(findAllOf1From1ToN(10000)); } private static void test6() { System.out.println(findAllOf1From1ToN(21345)); }}
有不妥当之处,麻烦告知:D
阅读全文
0 0
- 面试题32:整数中1出现的次数(从1到n整数中1出现的次数)
- 面试题32:从1到n整数中1出现的次数
- [剑指offer][面试题32]从1到n整数中1出现的次数
- 剑指Offer:面试题32 从1到n整数中1出现的次数
- 面试题32 从1到n整数中1出现的次数
- 剑指offer 面试题32—从1到n整数中1出现的次数
- 面试题32:从1到n整数中1出现的次数
- 剑指offer-面试题32:从1到n整数中1出现的次数
- 面试题32:从1到n整数中1出现的次数
- 算法 剑指Offer 面试题32 从1到n整数中1出现的次数
- 剑指Offer----面试题32:从1到n整数中1出现的次数
- 剑指offer面试题 32:从 1 到 n 整数中 1 出现的次数
- 面试题32:从1到n整数中1出现的次数
- 剑指offer面试题32:从1到n整数中1出现的次数
- 【剑指offer】面试题32-从1到n整数中1出现的次数
- 面试题32:从1到n整数中1出现的次数
- 剑指Offer系列-面试题32:从1到n整数中1出现的次数
- 剑指offer 面试题32 从 1 到 n 整数中 1 出现的次数
- BZOJ 1835 base 基站选址(DP 线段树)
- 牛客《剑指Offer》 -- 从尾到头打印链表
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- 启动Android studio,报错:aapt.exe
- 编译原理(七) 算符优先分析法(构造算符优先关系表算法及C++实现)
- 面试题 32: 从1到n 整数中1出现的次数
- mybatis中#{}和${}的区别
- 项目之日期转换
- keil&MDK封装函数到lib中,并在程序中调用lib
- Elasticsearch实战——全文检索架构设计
- 编译原理(八) 算符优先分析法(分析过程的算法和C++实现)
- Codeforces Gym 101173 J. Jazz Journey
- c#方法之 实例方法
- 正则表达式学习