当前位置:网站首页>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;
}
}
边栏推荐
- Interface automation framework, lm-easytest beta version released, use it quickly~
- 基于FPGA的DDS任意波形输出
- Flask框架——Flask-Mail邮件
- [Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
- 43.【list链表的定义及初始化】
- LeetCode_98_验证二叉搜索树
- 华为再发「天才少年」召集令!曾放弃360万年薪的他也来首秀
- eclipse连接SQL server数据库「建议收藏」
- BI-SQL丨WHILE
- 自动化办公|办公软件和亿图脑图MindMaster快捷键
猜你喜欢

【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码

Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose

去腾讯面试,直接让人出门左拐 :幂等性都不知道!

ECCV 2022 | Towards Data Efficient Transformer Object Detectors

Flask Framework - Sijax

Container sorting case
![[Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)](/img/65/7bd87794ebde510ecfd1b0e4bd4c94.png)
[Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)

Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked

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

华为7年经验的软件测试总监,给所有想转行学软件测试的朋友几点建议
随机推荐
开源工具推荐:高性能计算辅助工具MegPeak
Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates
吃透Chisel语言.28.Chisel进阶之有限状态机(二)——Mealy状态机及与Moore状态机的对比
3年软件测试经验面试要求月薪22K,明显感觉他背了很多面试题...
Flink优化
NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
双碳目标下:农田温室气体排放模拟
ECCV 2022 | Towards Data Efficient Transformer Object Detectors
机房布线的至高境界,美到窒息
How awesome is the "12306" architecture?
BI-SQL丨WHILE
Application of time series database in the field of ship risk management
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
Hello, World
What is defect analysis?An article takes you to understand the necessary skills of test engineers
Redis6.0 source code learning (5) ziplist
国内数字藏品的乱象与未来
业内人士真心话:只会测试没有前途的,我慌了......
跳槽前,把自己弄成卷王
Some thoughts on String