题解 | #表示数值的字符串#

表示数值的字符串

https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def isNumeric(self , str: str) -> bool:
        # write code here
        n = len(str)
        index = 0
        has_num = has_sign = has_e = has_dot = False

        while index<n and str[index] == ' ':
            index += 1
        while index<n:
            while index<n and '0'<=str[index]<='9':
                index += 1
                has_num = True
            if index == n:
                break
            if str[index] == 'e' or str[index] == 'E':
                if has_e or not has_num:
                    return False
                has_e = True
                has_num = has_sign = has_dot = False
            elif str[index]=='+' or str[index]=='-':
                if has_sign or has_num or has_dot:
                    return False
                has_sign = True
            elif str[index] == '.':
                if has_dot or has_e:
                    return False
                has_dot = True
            elif str[index] == ' ':
                break
            else:
                return False
            index += 1
        while index <n and str[index]==' ':
            index += 1
        return has_num and index == n

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-25 10:45
点赞 评论 收藏
分享
04-02 16:49
门头沟学院 Java
_bloodstream_:我也面了科大讯飞,主管面的时候听说急招人优先考虑能尽快实习的,我说忙毕设,后面就一直没消息了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务