当前位置:网站首页>剑指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
边栏推荐
- json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)
- Summary of common skills in H5 development of mobile terminal
- 图片随手机水平移动-陀螺仪。360度设置条件
- Interview questions and answers for the second company (2)
- Analysis of the transaction problem of chained method call
- JS对象赋值问题
- A semicolon is missing
- Okaleido ecological core equity Oka, all in fusion mining mode
- Database functions
- Redis Docker实例与数据结构
猜你喜欢

The problem of large fluctuation of hx711 data

js下载文件,FileSaver.js导出txt、excel文件

Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization

抽象工厂及其改进示例
![[Halcon vision] morphological corrosion](/img/f9/f01dd3340824ff28c84cf7bb52882e.png)
[Halcon vision] morphological corrosion
![[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数

【dectectron2】跟着官方demo一起做

Issue 5: the second essential skill for College Students

Issue 6: which mainstream programming language should college students choose

js翻页、kkpager.js翻页
随机推荐
Controller返回JSON数据
SAP ABAP 守护进程的实现方式
Uninstall Meizu app store
异常的概念与处理
Oracle创建索引
[Halcon vision] morphological corrosion
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
centos8(liunx)部署WTM(ASP.NET 5)使用pgsql
文案秘籍七步曲至----文献团队协作管理
Problems encountered in QRcode QR code (C language)
.net5wtm (asp.net core) PgSQL unpacking operation
Interview questions and answers of the first company (I)
反射机制简述
hx711 数据波动大的问题
C语言回调函数
Redis Docker实例与数据结构
Our Web3 entrepreneurship project is yellow
Unit test, what is unit test and why is it so difficult to write a single test
分布式锁解决方案之Redis实现
[Halcon vision] Fourier transform of image