当前位置:网站首页>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;
}
};
边栏推荐
- China sorbitol Market Forecast and investment strategy report (2022 Edition)
- sql刷题627. 变更性别
- C語言輸入/輸出流和文件操作
- How wild are hackers' ways of making money? CTF reverse entry Guide
- Activity的生命周期和启动模式详解
- 越来越多地使用 SLO 来实现可观测性|DevOps
- Template engine velocity Foundation
- What are the differences between PHP and DW
- Please, stop painting star! This has nothing to do with patriotism!
- Basic usage of Frida
猜你喜欢
How to solve the problem that the battery icon of notebook computer does not display
Girls who want to do software testing look here
How to solve the keyboard key failure of notebook computer
Installation and use of sqoop
SystemVerilog structure (II)
C语言输入/输出流和文件操作
模板引擎Velocity 基础
Transition technology from IPv4 to IPv6
The amazing open source animation library is not only awesome, but also small
Cookies and session keeping technology
随机推荐
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
China carbon disulfide industry research and investment strategy report (2022 Edition)
Redis6.0 新功能
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
Kali install Nessus
How to use F1 to F12 correctly on laptop keyboard
判断一棵二叉树是否为平衡二叉树
Rhcsa Road
Hidden Markov model (HMM): model parameter estimation
redis -- 数据类型及操作
【PyG】文档总结以及项目经验(持续更新
Concatenate strings to get the result with the smallest dictionary order
想做软件测试的女孩子看这里
String的trim()和substring()详解
【Try to Hack】vulnhub DC4
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
SystemVerilog structure (II)
How to solve the problem that the battery icon of notebook computer does not display
AI高考志愿填报:大厂神仙打架,考生付费围观
Judge whether a binary tree is a balanced binary tree