当前位置:网站首页>20. string representing numeric value
20. string representing numeric value
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 20. String representing the value
Ideas : Simulate traversal
Remove leading and trailing spaces
Define flag num,dot,exp
Traversal string
- If a character other than a valid character appears , Go straight back to false
- If the current character is a number , Set up num by true
- If the current character is
.- If it appears in
E and eor.after , namely dot or exp by true, return false - Set up dot by true
- If it appears in
- If the current character is
E or e- If no numbers have appeared before or have appeared
E or e, namely num by false or exp by true, return false - Set up exp by true,num by false, because e Must have a number after
- If no numbers have appeared before or have appeared
- If the current character is
+ and -, Judge whether it is the first character or e First character after
Traverse complete ,num It is the result.
class Solution {
public:
bool isNumber(string s) {
int startIdx=0,endIdx=s.size()-1;
while(startIdx<=endIdx&&s[startIdx]==' ')startIdx++;
while(startIdx<=endIdx&&s[endIdx]==' ')--endIdx;
if(startIdx>endIdx) return false;
bool dot=false,num=false,exp=false;
for(int i=startIdx;i<=endIdx;++i){
if(s[i]>='0'&&s[i]<='9'){
num=true;
}
else if(s[i]=='.'){
if(dot||exp)
return false;
dot=true;
}
else if(s[i]=='e'||s[i]=='E'){
if(exp||!num)
return false;
exp=true;
num=false;
}
else if(s[i]=='+'||s[i]=='-'){
if(i!=startIdx&&s[i-1]!='E'&&s[i-1]!='e')
return false;
}
else return false;
}
return num;
}
};
Time complexity O(n)
Spatial complexity O(1)
边栏推荐
- Thingsboard view telemetry data through database
- Harris corner detection principle-
- Rv1109 lvgl UI development
- The master programmer "plays" a C program that is not like C
- Main business objects of pupanvr record (5)
- Static keyword and inheritance, polymorphic and special classes
- IC验证中的force/release 学习整理(6)研究对 wire 类型信号的影响
- Pupanvr create project and management script (2)
- What is thinking
- Transpiration and evapotranspiration (ET) data, potential evapotranspiration, actual evapotranspiration data, temperature data, rainfall data
猜你喜欢
![[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)](/img/60/baebffb2024ddf5f2cb070f222b257.jpg)
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)

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

Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature

Servlet core

Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
![[backtracking based on bit operation] queen n problem 2](/img/d3/25b2ba7c49ce0a9c1de26bc5c9497b.jpg)
[backtracking based on bit operation] queen n problem 2

Microsoft announces that it will discontinue support for older versions of visual studio

Detailed analysis of mathematical modeling problem a (vaccine production scheduling problem) of May Day cup in 2021

Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class

Pupanvr- an open source embedded NVR system (1)
随机推荐
How to generate provincial data from county-level data in ArcGIS?
Nbiot module me3616 at command mqtt connecting thingsboard
Uview customer management JS
Surface net radiation flux data, solar radiation data, rainfall data, air temperature data, sunshine duration, water vapor pressure distribution, wind speed and direction data, surface temperature
1008 color classification
Calculation method notes for personal use
Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
Drive safety coding & troubleshooting guide
Force/release learning and sorting in IC Verification (6) research on the influence of wire type signals
LabVIEW關於TDMS和Binary存儲速度
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Token based authentication
Pupanvr- an open source embedded NVR system (1)
Fundamentals of intensive learning openai gym environment construction demo
CentOS compiling and installing mysql8.0
IC验证中的force/release 学习整理(5)研究对 reg类型信号的影响
Asp. Net core EF value conversion
Acquisition of Lai data, NPP data, GPP data and vegetation coverage data
Stm32f4 ll library multi-channel ADC
Qs100 at command mqtt access thingsboard