zoukankan      html  css  js  c++  java
  • 表示数值的字符串

    题目描述

    请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

    思路:

    分几种情况,括号代表可选
    整数 A:(+-)A
    小数部分B:(A).B
    小数+指数,指数部分C:(A).B e(E)(+-)C
    整数+指数:(+-)A e(E) (+-)C
    除去.+-eE,ABC都是整数
    .和e(E)后面必须有数字
    e,E前面必须有数字

    编程实现:

    python 代码1
     1 # -*- coding:utf-8 -*-
     2 class Solution:
     3     # s字符串
     4     def isNumeric(self, s):
     5         # write code here
     6         if len(s)==0:
     7             return False
     8         i = 0
     9         if s[i]=='+' or s[i]=='-':
    10             i += 1
    11         # 整数
    12         for j in range(i,len(s),1):
    13             if s[j]>='0' and s[j]<='9':
    14                 continue        
    15             if s[j]=='.':
    16                 # 进入小数判别:.前面可以没整数
    17                 return self.isfloat(s[j+1:])
    18             if s[j] == 'e' or s[j]=='E'  and j!=i:
    19                 #进入指数判别:e前面需要有整数,e后面可以有无+-
    20                 if j+1 >=len(s): # 需要判断j+1是否有效
    21                     return False
    22                 if s[j+1]=='+' or s[j+1]=='-':
    23                     j = j+1  
    24                 return self.isinteger(s[j+1:])
    25             else:
    26                 return False 
    27         return True # 只是整数
    28     def isfloat(self,s):
    29         # 小数后面可能加指数
    30         if len(s)==0:
    31             return False
    32         for j in range(len(s)):
    33             if s[j]>='0' and s[j]<='9':
    34                 continue        
    35             if s[j] == 'e' or s[j]=='E'  and j!= 0:
    36                 #进入指数判别:e前面需要有整数,e后面可以有无+-
    37                 if j+1 >=len(s): # e后面需要有整数
    38                     return False
    39                 if s[j+1]=='+' or s[j+1]=='-':
    40                     j = j+1
    41                 return self.isinteger(s[j+1:])
    42             else:
    43                 return False 
    44         return True # 只是小数     
    45     def isinteger(self,s):
    46         if len(s) ==0:
    47             return False
    48         for i in s:
    49             if i>='0' and i<='9':
    50                 continue
    51             else:
    52                 return False
    53         return True
    54         
    55             

    代码2

    使用正则表达式

    1 # -*- coding:utf-8 -*-
    2 import re
    3 class Solution:
    4     # s字符串
    5     def isNumeric(self, s):
    6         return re.match(r"^[+-]?[0-9]*(.[0-9]*)?([eE][+-]?[0-9]+)?$",s)
    7         # 或者如以下代码
    8        # import re
    9         #return re.match(r"^[+-]?[0-9]*(.[0-9]*)?([eE][+-]?[0-9]+)?$",s)

    其中

    ^ 匹配字符串的开头
    $ 匹配字符串的末尾。
    re* 匹配0个或多个的表达式。
    re+ 匹配1个或多个的表达式。
    re? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
  • 相关阅读:
    PAT (Advanced Level) 1114. Family Property (25)
    PAT (Advanced Level) 1113. Integer Set Partition (25)
    PAT (Advanced Level) 1112. Stucked Keyboard (20)
    PAT (Advanced Level) 1111. Online Map (30)
    PAT (Advanced Level) 1110. Complete Binary Tree (25)
    PAT (Advanced Level) 1109. Group Photo (25)
    PAT (Advanced Level) 1108. Finding Average (20)
    PAT (Advanced Level) 1107. Social Clusters (30)
    PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
    PAT (Advanced Level) 1105. Spiral Matrix (25)
  • 原文地址:https://www.cnblogs.com/shuangcao/p/12868411.html
Copyright © 2011-2022 走看看