当前位置:网站首页>LeetCode_数位枚举_困难_233.数字 1 的个数
LeetCode_数位枚举_困难_233.数字 1 的个数
2022-07-30 14:19:00 【小城老街】
1.题目
给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。
示例 1:
输入:n = 13
输出:6,对应的整数为 1、10、11、12、13(整数 11 中有 2 个 1)
示例 2:
输入:n = 0
输出:0
提示:
0 <= n <= 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-digit-one
2.思路
(1)枚举每一数位上 1 的个数
思路参考本题官方题解。
3.代码实现(Java)
//思路1————枚举每一数位上 11 的个数
class Solution {
public int countDigitOne(int n) {
int res = 0;
long mulK = 1;
//枚举每一数位上 1 的个数
for (int k = 0; n >= mulK; k++) {
res += (n / (mulK * 10)) * mulK + Math.min(Math.max(n % (mulK * 10) - mulK + 1, 0), mulK);
mulK *= 10;
}
return res;
}
}
边栏推荐
猜你喜欢

Remember an experience of interviewing an outsourcing company, should you go?

为什么做软件测试一定要学自动化?谈谈我眼中自动化测试的价值

时间序列的数据分析(四):STL分解

内容产品进化三板斧:流量、技术、产品形态

【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码

浅析显卡市场的未来走向:现在可以抄底了吗?

PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件

MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)

LoRaWAN网关源码分析(V1.0.2)

Desktop Software Development Framework Awards
随机推荐
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
Understand the Chisel language. 29. Chisel advanced communication state machine (1) - communication state machine: take the flash as an example
三电系统集成技术杂谈
MaxWell抓取数据
惊艳!京东T8纯手码的Redis核心原理手册,基础与源码齐下
从实例来看DAO:权力分散的伟大尝试
代码杂谈:从一道面试题看学会Rust的难度
Understand Chisel language. 28. Chisel advanced finite state machine (2) - Mealy state machine and comparison with Moore state machine
Container sorting case
5. DOM
自动化办公|办公软件和亿图脑图MindMaster快捷键
ECCV 2022 | 通往数据高效的Transformer目标检测器
NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
容器排序案例
The main content of terrain analysis (the special effect level of the wandering earth)
A new generation of open source free terminal tools, so cool
Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
redis6.0 源码学习(五)ziplist
A Small Case About Containers