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

考察点

递归,数字处理

知识点

题目

分析
这道题目要求1到n整数中1出现的次数,最简单的算法就是从1开始依次遍历到n,然后求每个数中1的个数(不停求余然后向右移位),这种算法的时间复杂度是O(nlogn),有没有更简单的算法呢?这种只有数字的题目思维一定要往针对每个位数处理上靠以及递归处理,比如21345这个数字,其实我们可以依次递归处理21345(134621345),1345(3461345),345(45345),45(545),5。针对21345把它分解为求最高位和剩下位数,最高位的话就是10000~19999部分,它的1的个数是10^(位数-1),当然如果最高位低于1的话那就直接是剩下位数代表的值了;剩下位数总共4位,每一位都可以固定为1,同时剩余位数的可能是0~9 10个数字,而且一定要记得和最高位相乘,因为最高位不同的时候整个值也不一样,最后递归处理其它数据即可

public class ThirtyTwo {
	public static void main(String[] args) {
		String str = "21345";
		System.out.println(getCountOne(str));
		System.out.println(getCount(str));
	}
	public static int getCount(String str) {
		if (str == null || str.length() == 0) {
			return 0;
		}
		if (str.length() == 1 && str.charAt(0) == '0') {
			return 0;
		}
		if (str.length() == 1 && str.charAt(0) == '1') {
			return 1;
		}
		int partA = 0;
		int first = str.charAt(0) - '0';
		if (first > 1) {
			partA = power(str.length() - 1);
		} else {
			partA = Integer.valueOf(str.substring(1)) + 1;
		}
		int partB = first * (str.length() - 1) * power(str.length() - 2);
		int partC = getCount(str.substring(1));
		return partA + partB + partC;
	}
	public static int power(int n) {
		int result = 1;
		for (int i = 0;i<n;i++) {
			result = result * 10;
		}
		return result;
	}
	public static int getCountOne(String str) {
		int val = Integer.valueOf(str);
		int count = 0;
		for (int i = 0;i<= val;i++) {
			int num = i;
			while(num > 0) {
				if (num % 10 == 1) {
					count++;
				}
				num /= 10;
			}
		}
		return count;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值