当前位置:网站首页>Sword finger offer (53): a string representing a numeric value
Sword finger offer (53): a string representing a numeric value
2022-07-26 10:46:00 【Konstanch】
solution 1: There may be a positive or negative ’+‘ perhaps ’-’. Next are several 0 To 9 The digits of represent the integer part of the value ( In some decimals, there may be no integer part of the value ). If the number is a decimal , Then there may be several after the decimal 0 To 9 The digits of represent the decimal part of the value . If the numerical value is expressed in scientific notation , Next is a ’e’ perhaps ’E’, And an integer immediately following ( It can have a plus or minus sign ) The index .
When judging whether a string conforms to the above pattern , First, check whether the first character is a sign . If it is , Move a character on the string , Continue scanning the remaining strings 0 To 9 The number of . If it is a decimal , You will encounter a decimal point . in addition , If it is a numerical value expressed by scientific notation , After integers or decimals, you may encounter ’e’ perhaps ’E’.
class Solution:
# s character string
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
边栏推荐
猜你喜欢

为什么需要自动化测试?软件测试师带你测评不同软件测试工具

Oracle cannot start tnslistener service cannot start

RT thread learning notes (V) -- edit, download and debug programs
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array

Phase 4: one of College Students' vocational skills preparation in advance

RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)

如何组装一个注册中心?
![[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
![Error[Pe147]: declaration is incompatible with '错误问题](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[Pe147]: declaration is incompatible with '错误问题

反射机制简述
随机推荐
Pengge C language lesson 4 (3)
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
Disable usbjatg in Altium Designer
1837.K进制表示下的各位数字总和
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
按二进制数中1的个数分类
Happens-Before原则深入解读
0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
flutter dart生成N个区间范围内不重复随机数List
Flutter集成极光推送
[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)
Pengge C language - minesweeping 2021-08-16
使用Selenium抓取zabbix性能监控图
Sql Server 数据库之初学体验
剑指Offer(四十三):左旋转字符串
Build ARM embedded development environment
回到顶部的几种方案(js)
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
sigmod 函数与softmax 函数对比
用两个栈实现队列