当前位置:网站首页>剑指Offer(五十三):表示数值的字符串
剑指Offer(五十三):表示数值的字符串
2022-07-26 10:36:00 【康斯坦奇】
解法1:在数值之前可能有一个表示正负的’+‘或者’-’。接下来是若干个0到9的数位表示数值的整数部分(在某些小数里可能没有数值的整数部分)。如果数值是一个小数,那么在小数后面可能会有若干个0到9的数位表示数值的小数部分。如果数值用科学记数法表示,接下来是一个’e’或者’E’,以及紧跟着的一个整数(可以有正负号)表示指数。
判断一个字符串是否符合上述模式时,首先看第一个字符是不是正负号。如果是,在字符串上移动一个字符,继续扫描剩余的字符串中0到9的数位。如果是一个小数,则将遇到小数点。另外,如果是用科学记数法表示的数值,在整数或者小数的后面还有可能遇到’e’或者’E’。
class Solution:
# s字符串
def isNumeric(self, s):
# write code here
if len(s) <= 0:
return False
has_sign = False
has_point = False
has_e = False
for i in range(len(s)):
if s[i] == 'E' or s[i] == 'e':
if has_e:
return False
else:
has_e = True
if i == len(s) - 1:
return False
elif s[i] == '+' or s[i] == '-':
if has_sign:
if s[i-1] != 'e' and s[i-1] !='E':
return False
else:
has_sign = True
if i > 0 and s[i-1] != 'e' and s[i-1] != 'E':
return False
elif s[i] == '.':
if has_point or has_e:
return False
else:
has_point = True
if i >0 and (s[i-1] == 'e' or s[i-1] == 'E'):
return False
else:
if s[i] < '0' or s[i] >'9':
return False
return True
边栏推荐
猜你喜欢

el-table实现可编辑表格

STM32 阿里云MQTT esp8266 AT命令
![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】

工厂模式详解

Uniapp uses the simple method signalr (only for web debugging, cannot package apps)

Issue 5: the second essential skill for College Students
![[leetcode每日一题2021/2/14]765. 情侣牵手](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode每日一题2021/2/14]765. 情侣牵手

vscode上使用anaconda(已经配置好环境)

sigmod 函数与softmax 函数对比

【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
随机推荐
.net operation redis set unordered collection
(转载)ArcGIS Engine中各种点的创建方法
[leetcode每日一题2021/4/23]368. 最大整除子集
剑指Offer(十):矩形覆盖
C语言回调函数
videojs转canvas暂停、播放、切换视频
粽子大战 —— 猜猜谁能赢
Analysis of the transaction problem of chained method call
【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
STM32 阿里云MQTT esp8266 AT命令
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
Database functions
.net operation redis string string
10 令 operator= 返回一个 reference to *this
SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
[转]ArcGIS中判断两个Geometry之间的关系
剑指Offer(九):变态跳台阶
记给esp8266烧录刷固件
Write to esp8266 burning brush firmware
13 以对象管理资源