题解 | #表示数值的字符串#
表示数值的字符串
https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8
import re
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param str string字符串
# @return bool布尔型
#
class Solution:
def isNumeric(self, str: str) -> bool:
# write code here
str = str.strip().lower()
def is_decimal(str):
matchObj = re.match(
r"([+-]?([1-9]+[0-9]*|[0-9]?)\.[0-9]+)|([+-]?([1-9]+[0-9]*|[0-9])\.[0-9]*)",
str,
)
if matchObj:
if matchObj.group(0) == str:
return True
return False
def is_int(str):
matchObj = re.match(r"[+-]?([1-9]+[0-9]*|[0-9])", str)
if matchObj:
if matchObj.group(0) == str:
return True
return False
def is_sci_notation_(str):
matchObj = re.match(
r"([+-]?([1-9][0-9]*|0))(\.)?([0-9]+)?(e[+-]?[0-9]+)?", str
)
if matchObj:
if matchObj.group(0) == str:
return True
return False
if is_decimal(str) or is_int(str) or is_sci_notation_(str):
return True
else:
return False

查看15道真题和解析
联想公司福利 1502人发布