当前位置:网站首页>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
边栏推荐
- 从蚂蚁的觅食过程看团队研发(转载)
- [leetcode daily question 2021/4/29]403. Frogs cross the river
- MySQL速学-2021-09-01
- RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)
- 20210807 1 c language program structure
- PLC与伺服电机连接
- 按二进制数中1的个数分类
- mysql20210906
- Constructors, method overloads, object arrays, and static
- 用两个栈实现队列
猜你喜欢

软件测试综述之软件测试的背景、实质、软件开发的过程

344.反转字符串

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2

PLC与伺服电机连接

工厂模式详解
![Error[Pe147]: declaration is incompatible with '错误问题](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[Pe147]: declaration is incompatible with '错误问题

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

RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)

349. 两个数组的交集
![[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
随机推荐
11 handle "self assignment" in operator=
35. 搜索插入位置
7-25 0-1背包 (50分)
英语基础句型结构------起源
如何组装一个注册中心?
2021-08-12函数递归_和鹏哥学习C语言
toolstrip 去边框
Summary of the seventh class of pengge C language
Sql Server之查询总结
0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
Issue 5: the second essential skill for College Students
Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
Happens-Before原则深入解读
2021-08-12 function recursion_ Learn C language with brother Peng
鹏哥C语言——扫雷2021-08-16
剑指Offer(七):斐波那契数列
按二进制数中1的个数分类
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming
SAP ABAP 守护进程的实现方式
2021-08-13 learn C language with pengge - array