LeetCode-Valid Number - 有限状态机
判断合法数字,之前好像在哪里看到过这题,
记得当时还写了好久,反正各种改,
今天看到了大神的解法(https://github.com/fuwutu/LeetCode/blob/master/Valid%20Number.cpp),
用有限状态机,非常简洁,不需要复杂的各种判断!
先枚举一下各种合法的输入情况:
1.空格+ 数字 +空格
2.空格+ 点 + 数字 +空格
3.空格+ 符号 + 数字 + 空格
4.空格 + 符号 + 点 + 数字 +空格
5.空格 + (1, 2, 3, 4) + e + (1, 2, 3, 4) +空格
组后合法的字符可以是:
1.数字
2.空格
有限状态机的状态转移过程:
起始为0:
当输入空格时,状态仍为0,
输入为符号时,状态转为3,3的转换和0是一样的,除了不能再接受符号,故在0的状态的基础上,把接受符号置为-1;
当输入为数字时,状态转为1, 状态1的转换在于无法再接受符号,可以接受空格,数字,点,指数;状态1为合法的结束状态;
当输入为点时,状态转为2,状态2必须再接受数字,接受其他均为非法;
当输入为指数时,非法;
状态1:
接受数字时仍转为状态1,
接受点时,转为状态4,可以接受空格,数字,指数,状态4为合法的结束状态,
接受指数时,转为状态5,可以接受符号,数字,不能再接受点,因为指数必须为整数,而且必须再接受数字;
状态2:
接受数字转为状态4;
状态3:
和0一样,只是不能接受符号;
状态4:
接受空格,合法接受;
接受数字,仍为状态4;
接受指数,转为状态5,
状态5:
接受符号,转为状态6,状态6和状态5一样,只是不能再接受符号,
接受数字,转为状态7,状态7只能接受空格或数字;状态7为合法的结束状态;
状态6:
只能接受数字,转为状态7;
状态7:
接受空格,转为状态8,状态7为合法的结束状态;
接受数字,仍为状态7;
状态8:
接受空格,转为状态8,状态8为合法的结束状态;

1 class Solution
2 {
3 public:
4 bool isNumber(const char *s)
5 {
6 enum InputType
7 {
8 INVALID, // 0
9 SPACE, // 1
10 SIGN, // 2
11 DIGIT, // 3
12 DOT, // 4
13 EXPONENT, // 5
14 NUM_INPUTS // 6
15 };
16
17 int transitionTable[][NUM_INPUTS] =
18 {
19 -1, 0, 3, 1, 2, -1, // next states for state 0
20 -1, 8, -1, 1, 4, 5, // next states for state 1
21 -1, -1, -1, 4, -1, -1, // next states for state 2
22 -1, -1, -1, 1, 2, -1, // next states for state 3
23 -1, 8, -1, 4, -1, 5, // next states for state 4
24 -1, -1, 6, 7, -1, -1, // next states for state 5
25 -1, -1, -1, 7, -1, -1, // next states for state 6
26 -1, 8, -1, 7, -1, -1, // next states for state 7
27 -1, 8, -1, -1, -1, -1, // next states for state 8
28 };
29
30 int state = 0;
31 while (*s != '\0')
32 {
33 InputType inputType = INVALID;
34 if (isspace(*s))
35 inputType = SPACE;
36 else if (*s == '+' || *s == '-')
37 inputType = SIGN;
38 else if (isdigit(*s))
39 inputType = DIGIT;
40 else if (*s == '.')
41 inputType = DOT;
42 else if (*s == 'e' || *s == 'E')
43 inputType = EXPONENT;
44
45 state = transitionTable[state][inputType];
46
47 if (state == -1)
48 return false;
49 else
50 ++s;
51 }
52
53 return state == 1 || state == 4 || state == 7 || state == 8;
54 }
55 };

LeetCode-Valid Number - 有限状态机的更多相关文章
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
- LeetCode——Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- Leetcode Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- LeetCode Valid Number 有效数字(有限自动机)
题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...
- leetcode - valid number 正则表达式解法
import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...
- [LeetCode] Valid Number 确认是否为数值
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- 《Linux内核分析》课程第四周学习总结
姓名:何伟钦 学号:20135223 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/U ...
- Linux内核分析(第五周)
系统调用的三层皮(下) 一.给MenuOs增加time和time-asm命令 ls rm menu -rf git clone xx(克隆新版本) cd menu make rootfs(自动编译生成 ...
- c++ 读写功能
课程作业三 git链接: Operations 感想 这次代码修改的地方主要有,加入了文件读写.读出功能,以及分离函数写到了头文件里. 但是也有很多不足的地方,首先本来想要 ...
- MongoDB ,cursor not found异常
查询mongoDB集合数据更新,数据有400w多.我一次用cursor(游标)取1w,处理更新.程序在某段时间运行中遍历游标时发生异常! DBCursor cursor = tabColl.find( ...
- windows32位系统 安装MongoDB
今天在win7 32位系统下安装 MongoDB 时,遇到了一堆坑,特此笔记. 一.下载MongoDB 打开官网下载地址:https://www.mongodb.com/download-center ...
- FileStream功能被禁用
今天还原数据库,遇到如下问题: 网上的解决方法大概是三种: 1.讲数据库备份文件权限设置为“EventOne” 2.打开SQLServer配置管理器,选中服务然后右击“属性”将FileStream相关 ...
- 伪数组(arguments及字符串)转数组的方法 贼简单
超简单的伪数组转数组的方法, 简单到令人发指! (这里伪数组使用arguments) 1.使用 Array.prototype.slice Array.prototype.slice.call(arg ...
- 51Nod 1287 加农炮 (线段树)
1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为M的正整数数组A,表示从左向右的地形高度 ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- MT【173】齐次消元单变量
已知$x\ge0,x^2+(y-2)^2=1,W=\dfrac{3x^2+2\sqrt{3}xy+5y^2}{x^2+y^2}$,求$W$的最值. 提示:$x\ne0$时,设$t=\dfrac{y}{ ...