https://leetcode-cn.com/problems/valid-number/
https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/
# -*- coding:utf-8 -*- class Solution(object): def isNumber(self, s): """ :type s: str :rtype: bool """ try: float(s) return True except: return False if __name__ == '__main__': s = '5e4' print(float(s)) print(Solution().isNumber(s))
思路:硬取巧。
这道题的正规思路应该是状态机,类似于设计模式中的状态机,维护一个状态的二维数组,表示当当前状态是i时,下一个状态是j时应该输出的状态。每次读取一个字符,依次向后判断。
// 数字,指数,正负,小数点,空格,其他字符
// 0 1 2 3 4 5
{1, 10, 8, 11, 0, 10},
{1, 3, 10, 2, 6, 10},
{7, 3, 10, 10, 6, 10},
{5, 10, 4, 10, 10, 10},
{5, 10, 10, 10, 10, 10},
{5, 10, 10, 10, 6, 10},
{10, 10, 10, 10, 6, 10},
{7, 3, 10, 10, 6, 10},
{9, 10, 10, 11, 10, 10},
{9, 3, 10, 2, 6, 10},
{10, 10, 10, 10, 10, 10},
{7, 10, 10, 10, 10, 10},
0 条评论