当前位置:网站首页>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;
}
}
边栏推荐
- 5. DOM
- 【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码
- The truth of the industry: I will only test those that have no future, and I panic...
- Flask Framework - Flask-Mail Mail
- kubernate部署服务
- Flask框架——Flask-SQLite数据库
- MPSK抗噪声性能对比(即MPSK标准误码率曲线)
- 桌面软件开发框架大赏
- Remember an experience of interviewing an outsourcing company, should you go?
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
猜你喜欢
【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码
Web3创始人和建设者必备指南:如何构建适合的社区?
43.【list链表的定义及初始化】
How to use Databricks for data analysis on TiDB Cloud | TiDB Cloud User Guide
Desktop Software Development Framework Awards
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
跳槽前,把自己弄成卷王
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
ToDesk版本更新,引入RTC传输技术,是否早以替代向日葵远程控制?
selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计
随机推荐
Web3创始人和建设者必备指南:如何构建适合的社区?
3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...
DocuWare 文件管理与工作流程自动化案例研究——DocuWare 工作流程功能使在家工作的员工能够保持沟通和高效工作,支持混合环境
我为何从开发人员转做测试,3年软件测试工程师,带你聊聊这其中的秘辛
43.【list的简单属性】
网站添加能换装可互动的live 2d看板娘
开源工具推荐:高性能计算辅助工具MegPeak
浅析显卡市场的未来走向:现在可以抄底了吗?
What is the relationship between the construction of smart cities and 5G technology in the new era
Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
华为再发「天才少年」召集令!曾放弃360万年薪的他也来首秀
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
A Small Case About Containers
Androd 跳转到google应用市场
查阅所连接过的WiFi所有信息(含密码)(访问历史所有WiFi连接)
新时代背景下智慧城市的建设与5G技术有何关联
SLF4J的使用
Understand Chisel language. 28. Chisel advanced finite state machine (2) - Mealy state machine and comparison with Moore state machine