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

考察点

java 复制代码
递归,数字处理

知识点

题目

分析

这道题目要求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个数字,而且一定要记得和最高位相乘,因为最高位不同的时候整个值也不一样,最后递归处理其它数据即可

java 复制代码
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;
	}
}
相关推荐
MATLAB代码顾问33 分钟前
Python实现蜂群算法优化TSP问题
开发语言·python·算法
jiushiapwojdap1 小时前
LU分解法求解线性方程组Matlab实现
数据结构·其他·算法·matlab
jinanwuhuaguo1 小时前
(第三十三篇)五月的文明奠基:OpenClaw 2026.5.2版本的文明级解读
android·java·开发语言·人工智能·github·拓扑学·openclaw
xmjd msup1 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
纽扣6672 小时前
【算法进阶之路】链表进阶:删除、合并、回文与排序全解析
数据结构·算法·链表
Cosolar2 小时前
一文吃透 LangChain&LangGraph:设计理念、框架结构与内部组件全拆解
人工智能·面试·架构
952362 小时前
SpringBoot统一功能处理
java·spring boot·后端
有一个好名字2 小时前
工具即双手 —— 从 Bash 到 Tool Dispatch Map
开发语言·chrome·bash
Lyyaoo.2 小时前
优惠券秒杀业务分析
java·开发语言
消失的旧时光-19432 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法