当前位置:网站首页>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;
}
}
边栏推荐
- [boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
- 从外企离开,我才知道什么叫尊重跟合规…
- iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
- DevOps可以帮助减少技术债务的十种方式
- Do you still have to rely on Simba to shout for a new business that is Kwai?
- Personal digestion of DDD
- Business process testing based on functional testing
- How to find out if the U disk file of the computer reinstallation system is hidden
- Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
- 使用yum来安装PostgreSQL13.3数据库
猜你喜欢

2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial

Newsletter L Huobi ventures is in-depth contact with genesis public chain

DAY FIVE

What can the interactive slide screen demonstration bring to the enterprise exhibition hall

Introduction au GPIO

GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析

Geo data mining (III) enrichment analysis of go and KEGG using David database

17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster

MATLIB从excel表中读取数据并画出函数图像

【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
随机推荐
【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
Yaduo Sangu IPO
What can the interactive slide screen demonstration bring to the enterprise exhibition hall
How about the order management of okcc call center
Pinia module division
Clipboard management tool paste Chinese version
Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
Common modification commands of Oracle for tables
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
Use source code compilation to install postgresql13.3 database
How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
DevOps可以帮助减少技术债务的十种方式
MySQL主从之多源复制(3主1从)搭建及同步测试
[automated testing framework] what you need to know about unittest
C language input / output stream and file operation [II]
为什么完全背包要用顺序遍历?简要解释一下
Pytest multi process / multi thread execution test case