当前位置:网站首页>剑指 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;
}
};边栏推荐
- How to restore the system with one click on Lenovo laptop
- Transition technology from IPv4 to IPv6
- SQL question brushing 627 Change gender
- China nylon 11 industry research and future forecast report (2022 Edition)
- PR basic clip operation / video export operation
- 单例模式的懒汉模式跟恶汉模式的区别
- ShenYu 网关开发:在本地启用运行
- Activity的生命周期和启动模式详解
- [jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
- Pytest learning notes (13) -allure of allure Description () and @allure title()
猜你喜欢
随机推荐
判断一棵二叉树是否为平衡二叉树
(1) CNN network structure
中国生物降解塑料市场预测与投资战略报告(2022版)
[C language supplement] judge which day tomorrow is (tomorrow's date)
Cookies and session keeping technology
Leetcode records - sort -215, 347, 451, 75
挖财学堂班主任给的证券账户安全吗?能开户吗?
How to use F1 to F12 correctly on laptop keyboard
Computed property “xxx“ was assigned to but it has no setter.
AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
多线程并发之CountDownLatch阻塞等待
What is the effect of choosing game shield safely in the game industry?
如何写出好代码 — 防御式编程指南
(27) Open operation, close operation, morphological gradient, top hat, black hat
The amazing open source animation library is not only awesome, but also small
【Try to Hack】vulnhub DC4
How to repair the laptop that cannot connect to the wireless network
China nylon 11 industry research and future forecast report (2022 Edition)
Template engine velocity Foundation
National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report









