当前位置:网站首页>43. Number of occurrences of 1 in 1 ~ n integers
43. Number of occurrences of 1 in 1 ~ n integers
2022-06-12 05:20:00 【Be your goat】
The finger of the sword Offer 43. 1~n In integers 1 Number of occurrences
Ideas : mathematics
Set the number n It's a x digit , remember n Of the i Position as n i n_i ni, Then you can put n Written as n x n x − 1 ⋯ n 2 n 1 n_xn_{x-1}\cdots n_2 n_1 nxnx−1⋯n2n1;
- remember n i n_i ni Is the current bit , Write it down as cur
- remember n i − 1 n i − 2 ⋯ n 2 n 1 n_{i-1}n_{i-2}\cdots n_2 n_1 ni−1ni−2⋯n2n1 by low
- remember n x n x − 1 ⋯ n i + 2 n i + 1 n_xn_{x-1}\cdots n_{i+2}n_{i+1} nxnx−1⋯ni+2ni+1 by high
- remember 1 0 i 10^i 10i It's a factor , Write it down as digit
According to the current bit cur Different , There are three situations :
1.cur=0 when :cur appear 1 The number of high decision , The formula is :
h i g h × d i g i t high\times digit high×digit
2.cur=1 when :cur appear 1 The number of times is determined by high and low decision , The formula is :
h i g h × d i g i t + l o w + 1 high\times digit+low+1 high×digit+low+1
3.cur=2,3,…9 when :cur appear 1 The number of high decision , The formula is :
( h i g h + 1 ) × d i g i t (high+1)\times digit (high+1)×digit
class Solution {
public:
int countDigitOne(int n) {
int high=n/10,cur=n%10,low=0,res=0;
long digit=1;
while(high||cur){
if(cur==0) res+=high*digit;
else if(cur==1) res+=high*digit+low+1;
else res+=(high+1)*digit;
low+=digit*cur,cur=high%10,high/=10,digit*=10;
}
return res;
}
};
Time complexity O(logn)
Spatial complexity O(1)
边栏推荐
- Introduction to audio alsa architecture
- Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
- Overview of common classes
- 1007- stair climbing
- Servlet core technology
- Sentinel-2 data introduction and download
- Asp. Net core EF value conversion
- Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data
- JS to determine whether the tags of multiple classes are empty
- Spatial distribution data of China's tertiary watershed / national new area distribution data /npp net primary productivity data / spatial distribution data of vegetation cover / land use data /ndvi d
猜你喜欢
![[cjson] precautions for root node](/img/6c/4462bc8126935f00a5c64f18de13ea.png)
[cjson] precautions for root node
![[backtracking based on bit operation] queen n problem 2](/img/d3/25b2ba7c49ce0a9c1de26bc5c9497b.jpg)
[backtracking based on bit operation] queen n problem 2

Map coordinate conversion of Baidu map API

How to deploy PostgreSQL as a docker container

It costs less than 30 yuan, but we still don't build it quickly - check the small knowledge of software application

Longest palindrome string

According to aiqicha, keep went public in Hong Kong and hit the "first share of online fitness"

How to count the total length of roads in the region and draw data histogram

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)

Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
随机推荐
Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ not found; SQL statement:
Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data
Acquisition of Lai data, NPP data, GPP data and vegetation coverage data
[backtracking] backtracking to solve subset problems
【cjson】根节点注意事项
Platform of ASoC framework driven by alsa
Main business objects of pupanvr record (5)
IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
Detailed tutorial on the use of yolov5 and training your own dataset with yolov5
Radiometric calibration and atmospheric correction of sentry 2 L1C multispectral data using sen2cor
Thingsboard view telemetry data through database
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Sentinel-2 data introduction and download
Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data
Servlet core technology
SQL transaction
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days
Advanced MySQL knowledge points (7)
Thingsboard create RCP widget