当前位置:网站首页>Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
2022-07-07 00:12:00 【Why is there a bug list】
topic
Enter an integer n , seek 1~n this n In the decimal representation of an integer 1 Number of occurrences .
for example , Input 12,1~12 These integers contain 1 The number of is 1、10、11 and 12,1 All in all 5 Time .
Example 1:
Input :n = 12
Output :5
Example 2:
Input :n = 13
Output :6
Limit :
1 <= n < 2^31
answer
class Solution {
public static int countDigitOne(int n) {
int digit = 1, res = 0;
int high = n / 10, cur = n % 10, low = 0;
while(high != 0 || cur != 0) {
if(cur == 0) res += high * digit;
else if(cur == 1) res += high * digit + low + 1;
else res += (high + 1) * digit;
low += cur * digit;
cur = high % 10;
high /= 10;
digit *= 10;
}
return res;
}
}
边栏推荐
- 1000 words selected - interface test basis
- 编译logisim
- Devops can help reduce technology debt in ten ways
- Introduction au GPIO
- 2022/2/10 summary
- [vector retrieval research series] product introduction
- Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
- C语言输入/输出流和文件操作【二】
- MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
- 17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
猜你喜欢
Gradle knowledge generalization
[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
Racher integrates LDAP to realize unified account login
2022/2/11 summary
Competition between public and private chains in data privacy and throughput
Cas d'essai fonctionnel universel de l'application
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
2022年PMP项目管理考试敏捷知识点(9)
Devops can help reduce technology debt in ten ways
DAY ONE
随机推荐
Unity color palette | color palette | stepless color change function
Clipboard management tool paste Chinese version
DAY THREE
How to answer the dualistic opposition of Zhihu
Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
Wind chime card issuing network source code latest version - commercially available
Wechat applet UploadFile server, wechat applet wx Uploadfile[easy to understand]
Introduction au GPIO
matplotlib画柱状图并添加数值到图中
Operation test of function test basis
2022/2/11 summary
web渗透测试是什么_渗透实战
MIT 6.824 - raft Student Guide
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
Local deployment Zeppelin 0.10.1
kubernetes部署ldap
App general function test cases
Why should a complete knapsack be traversed in sequence? Briefly explain
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?