博客
关于我
剑指 Offer 20. 表示数值的字符串
阅读量:704 次
发布时间: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/

    你可能感兴趣的文章
    HTTP 错误 500.21 - Internal Server Error 发布网站遇到这个错误
    查看>>
    初次安装webpack之后,提示安装webpack-cli
    查看>>
    使用FileZilla,FTP登录出现错误:FileZilla状态: 不安全的服务器,不支持 FTP over TLS
    查看>>
    Hbase压力测试
    查看>>
    StreamReader & StreamWriter
    查看>>
    C#中的类、方法和属性
    查看>>
    Python爬虫训练:爬取酷燃网视频数据
    查看>>
    Python数据分析入门(十九):绘制散点图
    查看>>
    Callable中call方法和Runnable中run方法的区别
    查看>>
    Linux yum提示Loaded plugins错误的解决方法
    查看>>
    Netty的体系结构及使用
    查看>>
    xshell解决文本粘贴格式错误
    查看>>
    什么是证券型代币?
    查看>>
    Android中获取并设置屏幕亮度
    查看>>
    MVVM_Template
    查看>>
    网络+图片加载框架(英文版)
    查看>>
    Python imageio方法示例
    查看>>
    Possible missing firmware
    查看>>
    JAVA BigInteger和BigDecimal类常用方式
    查看>>
    深度学习框架 各种模型下载集合 -- models list
    查看>>