当前位置:网站首页>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;
}
}
边栏推荐
- rancher集成ldap,实现统一账号登录
- DAY ONE
- DevOps可以帮助减少技术债务的十种方式
- 编译logisim
- Matplotlib draws a histogram and adds values to the graph
- 在Docker中分分钟拥有Oracle EMCC 13.5环境
- Devops can help reduce technology debt in ten ways
- Use source code compilation to install postgresql13.3 database
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- 从外企离开,我才知道什么叫尊重跟合规…
猜你喜欢
Business process testing based on functional testing
File and image comparison tool kaleidoscope latest download
Clipboard management tool paste Chinese version
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
【精品】pinia 基于插件pinia-plugin-persist的 持久化
ldap创建公司组织、人员
Gradle knowledge generalization
Core knowledge of distributed cache
[automated testing framework] what you need to know about unittest
48页数字政府智慧政务一网通办解决方案
随机推荐
PDF文档签名指南
Oracle EMCC 13.5 environment in docker every minute
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
Core knowledge of distributed cache
2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
三句话简要介绍子网掩码
Penetration test --- database security: detailed explanation of SQL injection into database principle
2022/2/11 summary
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
C language input / output stream and file operation [II]
The largest single investment in the history of Dachen was IPO today
Matplotlib draws a histogram and adds values to the graph
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
Huawei mate8 battery price_ Huawei mate8 charges very slowly after replacing the battery
DAY THREE
Use source code compilation to install postgresql13.3 database
STM32通过串口进入和唤醒停止模式
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
Data operation platform - data collection [easy to understand]