当前位置:网站首页>剑指 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;
}
};边栏推荐
- Judge whether the binary tree is a binary search tree
- Rhcsa Road
- unity3d扩展工具栏
- [Supplément linguistique c] déterminer quel jour est demain (date de demain)
- 【Try to Hack】vulnhub DC4
- Soft test software designer full truth simulation question (including answer analysis)
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- Hidden Markov model (HMM): model parameter estimation
- How to solve the keyboard key failure of notebook computer
- 中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
猜你喜欢

智能运维实战:银行业务流程及单笔交易追踪

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

Basic use of MySQL

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

模板引擎Velocity 基础

PR basic clip operation / video export operation

【PyG】文档总结以及项目经验(持续更新

多线程并发之CountDownLatch阻塞等待

可迭代对象与迭代器、生成器的区别与联系

Mysql database - Advanced SQL statement (2)
随机推荐
Advantages, values and risks of chain games compared with traditional games
What are the differences between PHP and DW
如何使用 etcd 实现分布式 /etc 目录
Redis 分布式鎖
GameFramework食用指南
Flux d'entrées / sorties et opérations de fichiers en langage C
模板引擎Velocity 基础
判断二叉树是否为二叉搜索树
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
Buuctf gold III
游戏行业安全选择游戏盾,效果怎么样?
Sword finger offer II 015 All modifiers in the string
The amazing open source animation library is not only awesome, but also small
存在安全隐患 起亚召回部分K3新能源
Leetcode records - sort -215, 347, 451, 75
Transition technology from IPv4 to IPv6
中国一次性卫生用品生产设备行业深度调研报告(2022版)
Pytest learning notes (13) -allure of allure Description () and @allure title()
Bugku's file contains
【flask入门系列】Cookie与Session