当前位置:网站首页>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;
}
}
边栏推荐
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- MIT 6.824 - Raft学生指南
- 【自动化测试框架】关于unittest你需要知道的事
- 三句话简要介绍子网掩码
- Oracle EMCC 13.5 environment in docker every minute
- Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
- Basic chart interpretation of "Oriental selection" hot out of circle data
- What is a responsive object? How to create a responsive object?
- Compilation of kickstart file
- STM32 enters and wakes up the stop mode through the serial port
猜你喜欢
GPIO簡介
陀螺仪的工作原理
48页数字政府智慧政务一网通办解决方案
How rider uses nuget package offline
MATLIB reads data from excel table and draws function image
Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
DAY SIX
Penetration test --- database security: detailed explanation of SQL injection into database principle
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
2022年PMP项目管理考试敏捷知识点(9)
随机推荐
Interface joint debugging test script optimization v4.0
How to find out if the U disk file of the computer reinstallation system is hidden
What is a responsive object? How to create a responsive object?
SQL的一种写法,匹配就更新,否则就是插入
Penetration test --- database security: detailed explanation of SQL injection into database principle
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Unity color palette | color palette | stepless color change function
DAY TWO
DAY TWO
iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
GPIO簡介
app通用功能測試用例
How to use vector_ How to use vector pointer
Do you still have to rely on Simba to shout for a new business that is Kwai?
Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
Things like random
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
Three application characteristics of immersive projection in offline display
Random类的那些事