zoukankan      html  css  js  c++  java
  • 「Leetcode」字符串相关问题选编

    剑指 Offer 20. 表示数值的字符串

    这题介绍一个使用有限自动机的做法,实际上细心的考虑corner case的效率比这个方法更好,但是这个方法比较具有代表性。
    画出如下的示意图:

    然后照着这个示意图做就可以了。
    注意,这么几种样例都是合法的:

    -1.e-12
    +.1
    +1.
    -.52E+3
    114.514e123
    

    接下来见代码:

    from enum import Enum
    class Solution:
        def isNumber(self, s: str) -> bool:
            State = Enum('State', (
                'START', 'SYM_START_DECIMAL_PNT', 'SYM_MID_DECIMAL_PNT', 'NUM_SIMPLE',
                'NUM_FIRST_PART', 'NUM_SECOND_PART', 'SYM_SCI', 'NUM_AFT_SCI', 
                'SYM_PN_SIMPLE', 'SYM_PN_SCI', 'ERROR'))
            end_state = (
                State.NUM_SIMPLE, State.SYM_MID_DECIMAL_PNT, State.NUM_FIRST_PART, 
                State.NUM_SECOND_PART, State.NUM_AFT_SCI)
    
            now_state = State.START
            s = s.strip()
            s_iter = iter(s)
            while True:
                if now_state == State.ERROR:
                    return False
                
                try:
                    now_chr = next(s_iter)
                except StopIteration:
                    if now_state in end_state:
                        return True
                    else:
                        return False
                
                if now_state == State.START:
                    if now_chr.isnumeric():
                        now_state = State.NUM_FIRST_PART
                        continue
                    elif now_chr in ['+', '-']:
                        now_state = State.SYM_PN_SIMPLE
                        continue
                    elif now_chr == ".":
                        now_state = State.SYM_START_DECIMAL_PNT
                        continue
                elif now_state == State.NUM_FIRST_PART:
                    if now_chr.isnumeric():
                        continue
                    elif now_chr == ".":
                        now_state = State.SYM_MID_DECIMAL_PNT
                        continue
                    elif now_chr in ["e", "E"]:
                        now_state = State.SYM_SCI
                        continue
                elif now_state == State.SYM_START_DECIMAL_PNT:
                    if now_chr.isnumeric():
                        now_state = State.NUM_SIMPLE
                        continue
                elif now_state == State.SYM_MID_DECIMAL_PNT:
                    if now_chr.isnumeric():
                        now_state = State.NUM_SECOND_PART
                        continue
                    elif now_chr in ["e", "E"]:
                        now_state = State.SYM_SCI
                        continue
                elif now_state == State.NUM_SIMPLE:
                    if now_chr in ["e", "E"]:
                        now_state = State.SYM_SCI
                        continue
                    elif now_chr.isnumeric():
                        continue
                elif now_state == State.NUM_SECOND_PART:
                    if now_chr.isnumeric():
                        continue
                    elif now_chr in ["e", "E"]:
                        now_state = State.SYM_SCI
                        continue
                elif now_state == State.SYM_SCI:
                    if now_chr.isnumeric():
                        now_state = State.NUM_AFT_SCI
                        continue
                    elif now_chr in ["+", "-"]:
                        now_state = State.SYM_PN_SCI
                        continue
                elif now_state == State.NUM_AFT_SCI:
                    if now_chr.isnumeric():
                        continue
                elif now_state == State.SYM_PN_SIMPLE:
                    if now_chr == ".":
                        now_state = State.SYM_START_DECIMAL_PNT
                        continue
                    elif now_chr.isnumeric():
                        now_state = State.NUM_FIRST_PART
                        continue
                elif now_state == State.SYM_PN_SCI:
                    if now_chr.isnumeric():
                        now_state = State.NUM_AFT_SCI
                        continue
                now_state = State.ERROR
    
  • 相关阅读:
    com.panie 项目开发随笔_前后端框架考虑(2016.12.8)
    Jsoup 使用教程:数据抽取
    Jsoup 使用教程:输入
    项目中图片处理总结
    jsonp 跨域请求
    由Memcached升级到 Couchbase的 Java 客户端的过程记录(三)
    由Memcached升级到 Couchbase的 Java 客户端的过程记录(二)
    jquery eval解析JSON中的注意点介绍
    JS禁止WEB页面鼠标事件大全
    jQuery事件之鼠标事件
  • 原文地址:https://www.cnblogs.com/samhx/p/leetcode-string.html
Copyright © 2011-2022 走看看