当前位置:网站首页>剑指 Offer 20. 表示数值的字符串
剑指 Offer 20. 表示数值的字符串
2022-07-01 16:46:00 【anieoo】
solution:
class Solution {
public:
bool isNumber(string s) {
//字符串空直接返回
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); //去掉首尾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') { //判定为数字,标记numFlag
numFlag = true;
} else if(s[i] == '.' && !dotFlag && !eFlag) { //判定为.需要之前没有出现过.和e,标记dotFlag
dotFlag = true;
} else if((s[i] == 'e' || s[i] == 'E') && !eFlag && numFlag) { //判定e或E的出现,需要前面没出现过e和已经出现过数字
eFlag = true;
numFlag = false;
} else if((s[i] == '+' || s[i] == '-') && (i == 0 || s[i - 1] == 'e' || s[i - 1] == 'E')) { //判定+或-的出现,只能出现在首位或者e的后面
} else return false;
}
return numFlag;
}
};
边栏推荐
- mysql -- explain性能优化
- P2893 [usaco08feb] making the grade g (DP & priority queue)
- SQL question brushing 584 Looking for user references
- Installation and use of sqoop
- 【PyG】文档总结以及项目经验(持续更新
- 【Kotlin】高阶函数介绍
- Soft test network engineer full truth simulation question (including answer and analysis)
- AI高考志愿填报:大厂神仙打架,考生付费围观
- 判断链表是否是回文链表
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
猜你喜欢
Jojogan practice
How to restore the system of Sony laptop
PR basic clip operation / video export operation
Installation and use of sqoop
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
为什么你要考虑使用Prisma
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
SQL question brushing 1050 Actors and directors who have worked together at least three times
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
随机推荐
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Redis 分布式锁
模板引擎Velocity 基礎
Rhcsa Road
SystemVerilog structure (II)
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
【C語言補充】判斷明天是哪一天(明天的日期)
What is the effect of choosing game shield safely in the game industry?
String class
中国一次性卫生用品生产设备行业深度调研报告(2022版)
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
Hidden Markov model (HMM): model parameter estimation
Girls who want to do software testing look here
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
China BMS battery management system Market Research Report (2022 Edition)
sql刷题627. 变更性别
Redis6.0 新功能
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
How to solve the keyboard key failure of notebook computer