当前位置:网站首页>Sword finger offer 20 String representing numeric value
Sword finger offer 20 String representing numeric value
2022-07-21 10:03:00 【Rare girl】
Use numbers , The sign , spot , size e The mutually exclusive relationship between , Simplified state machine , To solve the , Learned !
class Solution {
public:
bool isNumber(string s) {
if(s.size() == 0) return false;
// Skip leading and trailing spaces
int left = 0, right = s.length() - 1;
while(left <= right && s[left] == ' '){
left++;
}// The beginning of the string
if(left > right) return false;// Exclude strings that are all spaces
while(left < right && s[right] == ' '){
right--;
}// End of string
bool isNum = false;
bool isDot = false;
bool isEe = false;
bool isSign = false;
for(int i = left; i <= right; i++){
// Left closed right closed interval
if(s[i] >= '0' && s[i] <= '9'){
isNum = true;
}
// One '.';e/E Followed by an integer ( Can not have '.')
else if(s[i] == '.' && !isDot && !isEe){
isDot = true;
}
// One 'E' or 'e'; Numbers need to appear before
else if((s[i] == 'E' || s[i] == 'e') && isNum && !isEe){
isEe = true;
avoid e The situation at the end e It must be followed by an integer
isNum = false;
}
// '+''-' It can only appear at the beginning or 'E' or 'e' The last one of them
else if((s[i] == '+' || s[i] == '-') && (i == left || s[i - 1] == 'E' || s[i - 1] == 'e')){
isSign = true;
}
else{
return false;
}
}
// Must end with a number
return isNum;
}
};
边栏推荐
猜你喜欢
oracle is not null 过滤不了Null值
LeetCode 1260.二维网格迁移:两种方法解决(k次模拟/一步到位)
Using vant webapp in wechat applet
可以将网址在线打包封装成APK系统的完全开源源码
vscode安装及配置
Layoutinflater layout rendering tool
2022 new third-party pagoda panel btcloud PHP source code
[target detection] yolov1-v3 principle
[HMS core] [wallet kit] [solution] why can't Huawei wallet's client sample code run
TCP three handshakes and four waves dialysis
随机推荐
使用 Helm 3 包管理器在 Kubernetes 集群上安装软件
【无标题】
jquer $(‘div li‘) $(‘div,li‘) $(‘div>li‘) $(‘div‘,‘li‘)
DOS assembly branch, loop programming and register analysis
How do novices register for stock trading? Is it safe to open an account on great wisdom
js 数组reduce方法求和 求最大值 求最小值方法
Number of connected block midpoint (day 68)
擅长C(DAY 70)
2022河南萌新联赛第(二)场:河南理工大学 I - 22数
服务器内网、外网
可以将网址在线打包封装成APK系统的完全开源源码
CentOS 7 安装mysql
js 验证只能输入数字和一个小数点
LeetCode 1260.二维网格迁移:两种方法解决(k次模拟/一步到位)
AtomicInteger (计数器)的用法
SAP Spartacus 成功读取 Cart 之后,如何将 payload 插入全局 state
Measurement of typical periodic electrical signals
NFT链游戏系统开发元宇宙GameFi搭建
【HMS core】【Wallet Kit】【解决方案】华为钱包的客户端示例代码为何无法运行
恩智浦i.MX6Q开发板软硬件全开源提供核心板原理图