当前位置:网站首页>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;
}
}
边栏推荐
- OFDM Sixteen Lectures 3- OFDM Waveforms
- [Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
- (论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
- cookie模拟登录「建议收藏」
- Flink实时数仓完结
- 三电系统集成技术杂谈
- Machine learning difference in the competition and industry application
- 43.【list的简单属性】
- 地形分析的主要内容(流浪地球的特效水平)
- Web消息推送之SSE
猜你喜欢

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

网络安全——lcx的使用

Before quitting, make yourself a roll king

吃透Chisel语言.28.Chisel进阶之有限状态机(二)——Mealy状态机及与Moore状态机的对比

业内人士真心话:只会测试没有前途的,我慌了......

八年测试经验,为何惨遭领导痛批:你写的测试文档还不如刚来的应届生

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

Container sorting case

MPSK抗噪声性能对比(即MPSK标准误码率曲线)

Remember an experience of interviewing an outsourcing company, should you go?
随机推荐
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
Android jump to google app market
MySQL客户端工具的使用与MySQL SQL语句
Machine learning difference in the competition and industry application
查阅所连接过的WiFi所有信息(含密码)(访问历史所有WiFi连接)
LeetCode_98_验证二叉搜索树
Desktop Software Development Framework Awards
3年软件测试经验面试要求月薪22K,明显感觉他背了很多面试题...
Hello, World
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
sql中ddl和dml(sql与access的区别)
Digital signal processing course lab report (what foundation is needed for digital signal processing)
新时代背景下智慧城市的建设与5G技术有何关联
EasyV数字孪生流域|宁波智慧水利整体智治综合应用
ddl and dml in sql (the difference between sql and access)
SSE for Web Message Push
ECCV 2022 | Towards Data Efficient Transformer Object Detectors
从实例来看DAO:权力分散的伟大尝试
43.【list的简单属性】
Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing