当前位置:网站首页>剑指 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;
}
};
边栏推荐
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- Hi Fun Summer, play SQL planner with starrocks!
- Sword finger offer II 015 All modifiers in the string
- Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
- sql刷题586. 订单最多的客户
- 判断二叉树是否为二叉搜索树
- Redis Distributed Lock
- 阿里云、追一科技抢滩对话式AI
- 可迭代对象与迭代器、生成器的区别与联系
- AI高考志愿填报:大厂神仙打架,考生付费围观
猜你喜欢
游戏行业安全选择游戏盾,效果怎么样?
sql刷题586. 订单最多的客户
The amazing open source animation library is not only awesome, but also small
[pyg] document summary and project experience (continuously updated
SQL question brushing 627 Change gender
荣威 RX5 的「多一点」产品策略
In aks, use secret in CSI driver mount key vault
Basic use of MySQL
GameFramework食用指南
ShenYu 网关开发:在本地启用运行
随机推荐
Introduction to software engineering - Chapter 6 - detailed design
C语言输入/输出流和文件操作
What is the effect of choosing game shield safely in the game industry?
判断一棵二叉树是否为平衡二叉树
China nylon 11 industry research and future forecast report (2022 Edition)
Transition technology from IPv4 to IPv6
如何写出好代码 — 防御式编程指南
How to cancel automatic search and install device drivers for laptops
SQL question brushing 1050 Actors and directors who have worked together at least three times
求求你们,别再刷 Star 了!这跟“爱国”没关系!
越来越多地使用 SLO 来实现可观测性|DevOps
How to use F1 to F12 correctly on laptop keyboard
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
Redis6.0 new features
Jojogan practice
String类
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
Basic usage of Frida
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Rhcsa Road