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

表示数值的字符串

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

暴力全部模拟。写的时候能看出来很多重复的。不想改了。
class Solution:
    def isNumeric(self, str: str) -> bool:
        i, length = 0, len(str)
        while str[i] == " ":
            i += 1
            if i == length:
                return False
        if str[i] in "+-":
            i += 1
            if i == length:
                return False

        # 小数中的'.'+数字的情况,包括科学计数
        if str[i] == ".":
            i += 1
            if i == length:  # 时刻牢记i+=1之后要记得判断是否到尾部了
                return False
            if not "0" <= str[i] <= "9":
                return False
            while "0" <= str[i] <= "9":
                i += 1
                if i == length:
                    return True
            if str[i] == " ":
                while str[i] == " ":
                    i += 1
                    if i == length:
                        return True
                return False
            if str[i] in "eE":
                i += 1
                if i == length:
                    return False
                if str[i] in "+-":
                    i += 1
                    if i == length:
                        return False
                if not "0" <= str[i] <= "9":
                    return False
                while "0" <= str[i] <= "9":
                    i += 1
                    if i == length:
                        return True
                if str[i] == " ":
                    while str[i] == " ":
                        i += 1
                        if i == length:
                            return True
                    return False
                return False
            return False

        # 数字开头的情况三种(整数、数字+'.'、数字+'.'+数字)
        if "0" <= str[i] <= "9":
            while "0" <= str[i] <= "9":
                i += 1
                if i == length:
                    return True
            # 整数的情况,包括科学计数
            if str[i] == " ":
                while str[i] == " ":
                    i += 1
                    if i == length:
                        return True
                return False
            if str[i] in "eE":
                i += 1
                if i == length:
                    return False
                if str[i] in "+-":
                    i += 1
                    if i == length:
                        return False
                if not "0" <= str[i] <= "9":
                    return False
                while "0" <= str[i] <= "9":
                    i += 1
                    if i == length:
                        return True
                if str[i] == " ":
                    while str[i] == " ":
                        i += 1
                        if i == length:
                            return True
                    return False
                return False
            # 小数中的数字+'.'的情况,包括科学计数
            if str[i] == ".":
                i += 1
                if i == length:
                    return True
                if str[i] == " ":
                    while str[i] == " ":
                        i += 1
                        if i == length:
                            return True
                    return False
                if str[i] in "eE":
                    i += 1
                    if i == length:
                        return False
                    if str[i] in "+-":
                        i += 1
                        if i == length:
                            return False
                    if not "0" <= str[i] <= "9":
                        return False
                    while "0" <= str[i] <= "9":
                        i += 1
                        if i == length:
                            return True
                    if str[i] == " ":
                        while str[i] == " ":
                            i += 1
                            if i == length:
                                return True
                        return False
                    return False
                # 小数中的数字+'.'+数字的情况,包括科学计数
                if not "0" <= str[i] <= "9":
                    return False
                while "0" <= str[i] <= "9":
                    i += 1
                    if i == length:
                        return True
                if str[i] == " ":
                    while str[i] == " ":
                        i += 1
                        if i == length:
                            return True
                    return False
                if str[i] in "eE":
                    i += 1
                    if i == length:
                        return False
                    if str[i] in "+-":
                        i += 1
                        if i == length:
                            return False
                    if not "0" <= str[i] <= "9":
                        return False
                    while "0" <= str[i] <= "9":
                        i += 1
                        if i == length:
                            return True
                    if str[i] == " ":
                        while str[i] == " ":
                            i += 1
                            if i == length:
                                return True
                        return False
                    return False
                return False
            return False
        return False


全部评论

相关推荐

11-27 12:36
已编辑
门头沟学院 前端工程师
Apries:这个阶段来说,很厉害很厉害了,不过写的简历确实不是很行,优势删掉吧,其他的还行
点赞 评论 收藏
分享
jack_miller:我给我们导员说我不在这里转正,可能没三方签了。导员说没事学校催的时候帮我想办法应付一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务