当前位置:网站首页>LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数***
LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数***
2022-04-22 04:38:00 【暮雨林钟】
具体思路:
巧妙地利用数位的思想进行划分;

具体代码:
typedef long long ll;
class Solution {
public:
int countDigitOne(int n) {
ll k=1;//直接从个位开始计算;
ll cnt=0;
while(k<=n){
ll height=n/(k*10);//计算高位;
cnt+=height*k;
ll rest=n%(k*10);
ll self=rest/k;//当前位数:
if(self<2&&self>=1){
cnt+=rest-self*k+1;
}else if(self>=2){
cnt+=k;
}
k*=10;
}
return cnt;
}
};
版权声明
本文为[暮雨林钟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_66776925/article/details/124331958
边栏推荐
- How do I evaluate strings to numbers in Bash?
- Knowledge points related to compliance type
- Pgpool II 4.3 Chinese Manual - introductory tutorial
- labelme的常用命令
- Solution to Chinese translation of GoLand (in case of failure to download plug-ins)
- JVM tuning notes
- 链表第四篇
- pipeline communication
- ObjectMapper,别再像个二货一样一直new了
- 2022A特种设备相关管理(电梯)考试题模拟考试题库模拟考试平台操作
猜你喜欢

Objectmapper, stop being new like a second product

【S32K3_STM&PIT_MCAL】

crypto-js加密算法库【安装教程、缓存加密】

AT32 MCU Audio 24bit例程

Thread pool status + ThreadPoolExecutor

How did Jian Daoyun, who started a business in a small team of 10 people and now has an income of more than 100 million without financing, do it?

What are the main aspects of mobile app testing?

C entry - check the ID number with regular expressions.

论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..

【S32K3_STM&PIT_MCAL】
随机推荐
SCI论文写作--IEEE期刊的Word模板(LaTeX也有)
The website is linked to the gambling dark chain
Will the expired data of redis be deleted immediately?
排序一
Kotlin基础(十三) 嵌套类、内部类和匿名内部类
-bash: /home/lylg/bin/kf. sh: /bin/bash^M: bad interpreter: No such file or directory
(sip-1-phone registration) analysis of the whole process of IP phone registration to PBX telephone exchange through SIP Protocol - how to see the message in Wireshark
软件测试的测试方法你知道多少?
L1-052 2018 we want to win (5 points)
Matlab曲线的颜色、线型等参数设置方法
Redis 的过期数据会被立马删除么?
Sequence traversal of binary tree
spark 安装与使用 educoder
13.bufferevent接受和发送数据
NVIDIA, Wanxiang, fengyuzhu, dream chaser Fund... Talk about "new opportunities for yuan universe and industry" - 2022 yuan universe cloud Summit
Training summary report
链表(Linked List)第一篇
二叉树的层序遍历
Revit data processing (III)
[concurrent programming 043] how to solve the problems of CAS and ABA?