博客
关于我
剑指 Offer 20. 表示数值的字符串
阅读量:711 次
发布时间:2019-03-21

本文共 2470 字,大约阅读时间需要 8 分钟。

要判断一个字符串是否表示数值,我们可以使用有限状态机的方法,确保字符串符合数值的固定格式。数值可以分为整数和小数两种形式,分别有特定的结构要求。以下是详细的实现思路:

  • 初始化状态转移表:设置一个状态数组,每个状态表示当前处理的位置。状态之间的转移是基于字符串中的字符类型决定的。

  • 遍历字符串:对于每个字符,根据当前状态和字符类型,判断下一个状态。无法转移的情况则返回false。

  • 检查终止状态:在遍历结束后,检查当前状态是否为有效的结束状态。如果是,返回true,否则返回false。

  • 代码优化点:

    • 使用哈希表存储状态转移关系以提高效率。
    • 适当处理空格,允许在数值前后和中间出现。
    • 检查字符类型,确保仅允许数字、符号、点、e/E以及空格。
    • 处理特殊情况,如纯小数点、指数部分符号和数字。

    以下是具体的Python代码实现:

    import sysfrom collections import defaultdictdef is_number(s):    states = [        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int),        defaultdict(int)    ]        states[0][' '] = 0    states[0]['e'] = 8    states[0]['E'] = 8    states[5]['e'] = 7    states[5]['E'] = 7    states[6]['e'] = 8    states[6]['E'] = 8    states[7]['e'] = 8    states[7]['E'] = 8        states[1]['e'] = 7    states[1]['E'] = 7    states[2]['.'] = 4    states[3]['e'] = 7    states[3]['E'] = 7    states[4]['e'] = 7    states[4]['E'] = 7    states[5]['.] = 4    states[5]['e'] = 8    states[5]['E'] = 8        current_state = 0    for c in s:        c_lower = c.lower()        if c in '0123456789':            current_state = states[current_state].get(c, -1)            if current_state == -1:                return False        elif c == ' ':            current_state = states[current_state].get(c, -1)            if current_state == -1:                return False        elif c in '+-':            current_state = states[current_state].get(c, -1)            if current_state == -1:                return False        elif c == '.':            current_state = states[current_state].get(c, -1)            if current_state == -1:                return False        elif c in 'eE':            current_state = states[current_state].get(c, -1)            if current_state == -1:                return False        else:            return False                if current_state == -1:            return False                if current_state not in (2, 3, 4, 7, 8):            continue                if current_state not in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):            return False        return current_state in (2, 3, 4, 7, 8)

    示例验证:

    • "0" → true| 初始化状态为0,处理字符0,转移到状态0,最后返回true。
    • "e" → false| 处理e时,无法从初始状态转移到允许状态,故返回false。
    • "." → false| 初始化处理点转移到状态8,但未转移到继续,返回false。
    • " .1 " → true| 处理空格后转移到状态0,再处理点转为状态8,然后1转为状态4,最终返回true。

    通过有限状态机的方法,实现了对数值字符串的有效性判断,确保字符串符合所有规定的格式。

    转载地址:http://wtjrz.baihongyu.com/

    你可能感兴趣的文章
    Python函数运行的可执行文件的终端输出如何以一般方式静音?
    查看>>
    Python+Pytest+Allure+Git+Jenkins接口自动化框架
    查看>>
    python+pytest接口自动化 —— 参数关联
    查看>>
    python+pytest接口自动化 —— 参数关联
    查看>>
    Python+pytest接口自动化 —— 接口测试基础
    查看>>
    python+pytest接口自动化 —— 自动化用例编写思路 (使用pytest编写一个测试脚本)
    查看>>
    Python+pytest接口自动化之cookie绕过登录(保持登录状态)
    查看>>
    python+pytest接口自动化:接口测试
    查看>>
    Python+requests+unittest执行接口自动化测试详情
    查看>>
    Python+requests+unittest执行接口自动化测试详情
    查看>>
    python+requests+unittest执行自动化接口测试!
    查看>>
    Python+Requests编码识别Bug
    查看>>
    python函数编写_[零基础学python]传说中的函数编写条规
    查看>>
    Python+selenium —— UI自动化之鼠标操作
    查看>>
    python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
    查看>>
    Python+Selenium+Threading进行兼容性测试
    查看>>
    Python+selenium+unittest的GUI自动化框架实现
    查看>>
    python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
    查看>>
    python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
    查看>>
    Python+Selenium之数据驱动测试的实现
    查看>>