当前位置:网站首页>剑指 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;
}
};边栏推荐
- sql刷题584. 寻找用户推荐人
- PR basic clip operation / video export operation
- 英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
- Concatenate strings to get the result with the smallest dictionary order
- The amazing open source animation library is not only awesome, but also small
- SystemVerilog structure (II)
- 判断二叉树是否为二叉搜索树
- Rhcsa Road
- 荣威 RX5 的「多一点」产品策略
- China BMS battery management system Market Research Report (2022 Edition)
猜你喜欢

重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标

Buuctf gold III

C language input / output stream and file operation

Detailed explanation of activity life cycle and startup mode

在MeterSphere接口测试中如何使用JMeter函数和MockJS函数

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"

PR basic clip operation / video export operation

National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report

SQL question brushing 584 Looking for user references

Leetcode records - sort -215, 347, 451, 75
随机推荐
China carbon disulfide industry research and investment strategy report (2022 Edition)
【flask入门系列】Cookie与Session
sql刷题1050. 合作过至少三次的演员和导演
多线程并发之CountDownLatch阻塞等待
Stegano in the world of attack and defense
C語言輸入/輸出流和文件操作
Template Engine Velocity Foundation
How to solve the keyboard key failure of notebook computer
Installation and use of sqoop
软件工程导论——第六章——详细设计
Rhcsa Road
Sword finger offer II 015 All modifiers in the string
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
How to maintain the laptop battery
判断二叉树是否为二叉搜索树
China BMS battery management system Market Research Report (2022 Edition)
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
(28) Shape matching based on contour features
【Kotlin】高阶函数介绍