当前位置:网站首页>剑指 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 question brushing 586 Customers with the most orders
- Soft test software designer full truth simulation question (including answer analysis)
- Soft test network engineer full truth simulation question (including answer and analysis)
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- Computed property “xxx“ was assigned to but it has no setter.
- China nylon 11 industry research and future forecast report (2022 Edition)
- SQL question brushing 627 Change gender
- 【PyG】文档总结以及项目经验(持续更新
- 模板引擎Velocity 基礎
- The amazing open source animation library is not only awesome, but also small
猜你喜欢

走进微信小程序

ACL 2022 | 分解的元学习小样本命名实体识别

Installation and use of sqoop

Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!

(27) Open operation, close operation, morphological gradient, top hat, black hat
![[pyg] document summary and project experience (continuously updated](/img/b4/75da8c3e657069be4e3e3bfd5b2dc0.png)
[pyg] document summary and project experience (continuously updated

The amazing open source animation library is not only awesome, but also small

Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI

模板引擎Velocity 基礎

SQL question brushing 586 Customers with the most orders
随机推荐
What is the effect of choosing game shield safely in the game industry?
Gold, silver and four want to change jobs, so we should seize the time to make up
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
How to repair the laptop that cannot connect to the wireless network
Determine whether the linked list is a palindrome linked list
Redis6.0 新功能
Dataframe gets the number of words 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
存在安全隐患 起亚召回部分K3新能源
Hidden Markov model (HMM): model parameter estimation
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
How wild are hackers' ways of making money? CTF reverse entry Guide
[pyg] document summary and project experience (continuously updated
【flask入门系列】Cookie与Session
ACL 2022 | 分解的元学习小样本命名实体识别
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
How to restore the system with one click on Lenovo laptop
【PyG】文档总结以及项目经验(持续更新
(12) About time-consuming printing
Advantages, values and risks of chain games compared with traditional games