当前位置:网站首页>Sword finger offer 20 String representing numeric value
Sword finger offer 20 String representing numeric value
2022-07-01 17:02:00 【anieoo】
Original link : The finger of the sword Offer 20. String representing the value
solution:
class Solution {
public:
bool isNumber(string s) {
// String empty direct return
if(s.empty()) return false;
int i = 0,j = s.size() - 1;
while(s[i] == ' ' && i < j) i++;
while(s[j] == ' ' && i < j) j--;
s = s.substr(i, j - i + 1); // Remove the head and tail 0
bool numFlag = false;
bool dotFlag = false;
bool eFlag = false;
for(int i = 0;i < s.size();i++) {
if(s[i] >= '0' && s[i] <= '9') { // It's a number , Mark numFlag
numFlag = true;
} else if(s[i] == '.' && !dotFlag && !eFlag) { // Judged as . There is no need before . and e, Mark dotFlag
dotFlag = true;
} else if((s[i] == 'e' || s[i] == 'E') && !eFlag && numFlag) { // determine e or E Appearance , Need not appear before e And numbers have appeared
eFlag = true;
numFlag = false;
} else if((s[i] == '+' || s[i] == '-') && (i == 0 || s[i - 1] == 'e' || s[i - 1] == 'E')) { // determine + or - Appearance , Can only appear in the first place or e Behind
} else return false;
}
return numFlag;
}
};边栏推荐
- Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
- 【C语言基础】12 字符串
- 多线程并发之CountDownLatch阻塞等待
- Rhcsa Road
- sql刷题627. 变更性别
- Buuctf gold III
- Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
- Detailed explanation of activity life cycle and startup mode
- AI高考志愿填报:大厂神仙打架,考生付费围观
- Yyds dry inventory MySQL RC transaction isolation level implementation
猜你喜欢

Leetcode records - sort -215, 347, 451, 75

sql刷题1050. 合作过至少三次的演员和导演

Machine learning 11 clustering, outlier discrimination

Soft test network engineer full truth simulation question (including answer and analysis)

Introduction to software engineering - Chapter 6 - detailed design

Redis distributed lock

How to use F1 to F12 correctly on laptop keyboard

换掉UUID,NanoID更快更安全!

There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds

Activity的生命周期和启动模式详解
随机推荐
Determine whether the linked list is a palindrome linked list
P2893 [usaco08feb] making the grade g (DP & priority queue)
越来越多地使用 SLO 来实现可观测性|DevOps
中国生物降解塑料市场预测与投资战略报告(2022版)
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
String的trim()和substring()详解
Redis6.0 new features
Soft test software designer full truth simulation question (including answer analysis)
Stegano in the world of attack and defense
[kotlin] Introduction to higher-order functions
Rhcsa Road
Kali install Nessus
[C language foundation] 12 strings
How to restore the system with one click on Lenovo laptop
Redis distributed lock
软件工程导论——第六章——详细设计
中国茂金属聚乙烯(mPE)行业研究报告(2022版)
中国PBAT树脂市场预测及战略研究报告(2022版)
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)