当前位置:网站首页>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;
}
}
边栏推荐
- 【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码
- 什么是缺陷分析?一篇文章带你了解,测试工程师必备技能
- A simple change for problem, knapsack problem sets of shell
- NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
- Skywalking入门
- postgresql的普通字符串和转义字符串
- 基于FPGA的DDS任意波形输出
- 算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
- Recommended open source tools: MegPeak, a high-performance computing tool
- The truth of the industry: I will only test those that have no future, and I panic...
猜你喜欢
随机推荐
eclipse连接SQL server数据库「建议收藏」
双碳目标下:农田温室气体排放模拟
吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例
localhost与127.0.0.1
How to use Databricks for data analysis on TiDB Cloud | TiDB Cloud User Guide
以unity3d为例解读:游戏数据加密
LoRaWAN网关源码分析(V1.0.2)
网站添加能换装可互动的live 2d看板娘
Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates
ROS 导航
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
记面试外包公司的一次经历,到底该不该去?
JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )
(论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
使用bat脚本以json格式导出mongo数据库中指定表数据
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
CVE-2022-33891 Apache Spark 命令注入复现
Recommended open source tools: MegPeak, a high-performance computing tool
71页全域旅游综合整体解决方案2021 ppt
Conversion between pytorch and keras (the code takes LeNet-5 as an example)








