65有效数字

验证给定的字符串是否可以解释为十进制数字。

例如:

"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。这里给出一份可能存在于有效十进制数字中的字符列表:

  • 数字 0-9
  • 指数 - “e”
  • 正/负号 - “+”/”-“
  • 小数点 - “.”
  • 当然,在输入中,这些字符的上下文也很重要。

更新于 2015-02-10: C++函数的形式已经更新了。如果你仍然看见你的函数接收 const char * 类型的参数,请点击重载按钮重置你的代码。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/valid-number 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Solution 1

有限状态机

class Solution:
    def isNumber(self, s: str) -> bool:
        state = 0
        """
        state details (when at if clause):
            0 initial state or all characters before are ' '
            1 the character before is '+' or '-' and before '.' 'e'
            2 the character before in 0~9 and before '.' 'e'
            3 the character before is '.' and there is number before '.'
            4 the character before is '.' and there is no number before '.'
            5 the character before in 0~9 and after '.' before 'e'
            6 the character before is 'e'
            7 the character before is '+' or '-' and after '.' 'e'
            8 the character before in 0~9 and after '.' 'e'
            9 the character before is ' ' and there is already a qualified number
        """
        for c in s:
            if (c == ' '):
                if state in [1, 3, 6, 7]:
                    return False
                elif state in [0, 9]:
                    pass
                elif state in [2, 4, 5, 8]:
                    state = 9
                else:
                    raise Exception("c=' '")
            elif (c in ['+', '-']):
                if state in [1, 2, 3, 4, 5, 7, 8, 9]:
                    return False
                elif state == 0:
                    state = 1
                elif state == 6:
                    state = 7
                else:
                    raise Exception("c='+'/'-'")
            elif (c in list(map(str, range(10)))):
                if state == 9:
                    return False
                elif state in [2, 5, 8]:
                    pass
                elif state in [0, 1]:
                    state = 2
                elif state in [3, 4]:
                    state = 5
                elif state in [6, 7]:
                    state = 8
                else:
                    raise Exception("c=0~9")
            elif (c == '.'):
                if state in [3, 4, 5, 6, 7, 8, 9]:
                    return False
                elif state in [0, 1]:
                    state = 3
                elif state == 2:
                    state = 4
                else:
                    raise Exception("c='.'")
            elif (c == 'e'):
                if state in [0, 1, 3, 6, 7, 8, 9]:
                    return False
                elif state in [2, 4, 5]:
                    state = 6
                else:
                    raise Exception("c=0~9")
            else:
                # raise Exception(f"unknown char {c}")
                return False
        if (state in [2, 4, 5, 8, 9]):
            return True
        else:
            return False