当前位置:网站首页>剑指 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;
}
};边栏推荐
- Leetcode 77 combination -- backtracking method
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- Introduction to software engineering - Chapter 6 - detailed design
- 【Try to Hack】vulnhub DC4
- How to restore the system with one click on Lenovo laptop
- Hidden Markov model (HMM): model parameter estimation
- Determine whether the linked list is a palindrome linked list
- 阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
- AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
- 【C語言補充】判斷明天是哪一天(明天的日期)
猜你喜欢

【C补充】【字符串】按日期排序显示一个月的日程

How to solve the problem that the battery icon of notebook computer does not display

Computed property “xxx“ was assigned to but it has no setter.

Pytest learning notes (13) -allure of allure Description () and @allure title()

Rhcsa Road

sql刷题1050. 合作过至少三次的演员和导演

模板引擎Velocity 基础

Shenyu gateway development: enable and run locally

Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"

sql刷题627. 变更性别
随机推荐
Detailed explanation of activity life cycle and startup mode
Bugku's file contains
ShenYu 网关开发:在本地启用运行
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Soft test software designer full truth simulation question (including answer analysis)
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
SQL question brushing 584 Looking for user references
C language input / output stream and file operation
Leetcode records - sort -215, 347, 451, 75
[flask introduction series] cookies and session
Rhcsa Road
多线程并发之CountDownLatch阻塞等待
(28) Shape matching based on contour features
游戏行业安全选择游戏盾,效果怎么样?
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
mysql -- explain性能优化