当前位置:网站首页>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;
}
}
边栏推荐
- 谷歌百度雅虎都是中国公司开发的通用搜索引擎_百度搜索引擎url
- 《LaTex》LaTex数学公式简介「建议收藏」
- 数据运营平台-数据采集[通俗易懂]
- Automatic test tool katalon (WEB) test operation instructions
- Use type aliases in typescript
- Design of short chain
- 【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
- Server SMP, NUMA, MPP system learning notes.
- Pinia module division
- 刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
猜你喜欢
什么是响应式对象?响应式对象的创建过程?
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
How to find out if the U disk file of the computer reinstallation system is hidden
Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
The largest single investment in the history of Dachen was IPO today
1000字精选 —— 接口测试基础
DAY FIVE
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
量子时代计算机怎么保证数据安全?美国公布四项备选加密算法
2022/2/11 summary
随机推荐
Why should a complete knapsack be traversed in sequence? Briefly explain
okcc呼叫中心的订单管理时怎么样的
ldap创建公司组织、人员
app通用功能测试用例
Building lease management system based on SSM framework
C语言输入/输出流和文件操作【二】
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
[212] what are three methods for PHP to send post requests
App general function test cases
app通用功能測試用例
Common modification commands of Oracle for tables
How to find out if the U disk file of the computer reinstallation system is hidden
【精品】pinia 基于插件pinia-plugin-persist的 持久化
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
MIT 6.824 - Raft学生指南
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
【向量检索研究系列】产品介绍
DAY TWO
【自动化测试框架】关于unittest你需要知道的事
rancher集成ldap,实现统一账号登录