题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
思路:
A.BeC
- A:为数值的整数部分,可以以“+”或者“-”开头的0-9的数位串
- B:是紧跟着小数点的小数部分,是0-9的数位串
- C:是紧跟着E或者e的指数部分,可以以“+”或者“-”开头的0-9的数位串
# -*- coding:utf-8 -*-
class Solution:
# s字符串
def isNumeric(self, s):
# write code here
isdot = True
ise = True
if len(s)==0:
return False
for i in range(len(s)):
if s[i] in '+-' and (i==0 or s[i-1] in 'eE'):
continue
elif s[i] in '.' and isdot:
isdot = False
#判断小数点后面是否跟的数字
if i>=len(s)-1 or s[i+1] not in '012346789':
return False
elif s[i] in 'Ee' and ise:
ise = False
isdot = False
if i>=len(s)-1 or s[i+1] not in '-+012346789':
return False
elif s[i] not in '0123456789':
return False
return True
s = Solution()
print s.isNumeric("12e+5.4")
网友评论