确定有限自动机 valid number
原题地址:http://oj.leetcode.com/problems/valid-number/
题意:判断输入的字符串是否是合法的数。
解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅。本文参考了http://www.cnblogs.com/zuoyuan/p/3703075.html里面的内容,在此致谢!
先要知道到底哪些数是合理的。
e之前呢。
123
.123
123.456
123.
+123
+.123
e之后
123
+123
首先这个题有9种状态:
0初始无输入或者只有space的状态
1输入了数字之后的状态
2前面无数字,只输入了dot的状态
3输入了符号状态
4前面有数字和有dot的状态
5'e' or 'E'输入后的状态
6输入e之后输入Sign的状态
7输入e后输入数字的状态
8前面有有效数输入之后,输入space的状态
共9种状态了,难设计的是6,7,8状态。
分好之后就好办了,设计出根据输入进行状态转换就OK了。
这里的输入可以分:
INVALID=0;#无效输入包括: Alphas, '(', '&' ans so on
SPACE=1
SIGN=2 # '+' or '-'
DIGIT=3 # numbers
DOT=4 # '.'
EXPONENT=5 # 'e' or 'E'
转移矩阵A(9X6)如下:
-1, 0, 3, 1, 2, -1
-1, 8, -1, 1, 4, 5
-1, -1, -1, 4, -1, -1
-1, -1, -1, 1, 2, -1
-1, 8, -1, 4, -1, 5
-1, -1, 6, 7, -1, -1
-1, -1, -1, 7, -1, -1
-1, 8, -1, 7, -1, -1
-1, 8, -1, -1, -1, -1
行代表了9种状态,列代表了6种输入方式也就是6种跳转方式。举个例子:A[0][2]=3,这有什么含义呢?意思是:第0种状态为【0初始无输入或者只有space的状态】,在输入第2种输入【SIGN=2 # '+' or '-'】后,会跳转到第3种状态【3输入了符号状态】。A[1][1]=8是什么意思呢?意思是:第1种状态为【1输入了数字之后的状态】,在输入第1种输入【SPACE=1】后,跳转到了第8种状态【8前面有有效数输入之后,输入space的状态】。
根据以上的解释,大家应该明白什么事状态间的跳转了,这个共9种状态,所以是确定有穷自动机。其实难点在于状态的分割,要把每种情况都想到。
而这9种状态中:只有1、4、7、8这四种状态合法,所以最后state跳转到这四种状态之一时,说明输入是合法的!
状态转移图 from leetcode discuss:
代码:
按 Ctrl+C 复制代码
class Solution:
# @param s, a string
# @return a boolean
# @finite automation
def isNumber(self, s):
INVALID=0; SPACE=1; SIGN=2; DIGIT=3; DOT=4; EXPONENT=5;
#0invalid,1space,2sign,3digit,4dot,5exponent,6num_inputs
transitionTable=[[-1, 0, 3, 1, 2, -1], #0 no input or just spaces
[-1, 8, -1, 1, 4, 5], #1 input is digits
[-1, -1, -1, 4, -1, -1], #2 no digits in front just Dot
[-1, -1, -1, 1, 2, -1], #3 sign
[-1, 8, -1, 4, -1, 5], #4 digits and dot in front
[-1, -1, 6, 7, -1, -1], #5 input 'e' or 'E'
[-1, -1, -1, 7, -1, -1], #6 after 'e' input sign
[-1, 8, -1, 7, -1, -1], #7 after 'e' input digits
[-1, 8, -1, -1, -1, -1]] #8 after valid input input space
state=0; i=0
while i<len(s):
inputtype = INVALID
if s[i]==' ': inputtype=SPACE
elif s[i]=='-' or s[i]=='+': inputtype=SIGN
elif s[i] in '0123456789': inputtype=DIGIT
elif s[i]=='.': inputtype=DOT
elif s[i]=='e' or s[i]=='E': inputtype=EXPONENT state=transitionTable[state][inputtype]
if state==-1: return False
else: i+=1
return state == 1 or state == 4 or state == 7 or state == 8
确定有限自动机 valid number的更多相关文章
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
- Valid Number @python
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 配置域名服务器报错named[822]: dns_rdata_fromtext /etc/bind/db.asertest.com mail not a valid number
问题描述: 为了配置邮件服务器,更改了相关域名,改完后,重启bind9报错 Mar 17 14:39:39 DnsServer2 named[822]: dns_rdata_fromtext: /et ...
- [Swift]LeetCode65. 有效数字 | Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
随机推荐
- 如何让你的Ssh连接,更加安全?
希望你会涨姿势. First: vim /etc/ssh/sshd_config 在Port 22下面加一行,以端口1438为例,Port 1438 然后保存,重启ssh服务 systemctl re ...
- MySql EF6 DBFirst 向导无法生成 edmx 解决方法(同:您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库提供程序)
使用 MySql EF6 DBfirst 生成模型时经常会遇到EF6模式无法选择的情况究其原因, 还是因为没有正确的使用 Connector/Net. 下面说一下使用方法. 使用 MySql DBFi ...
- 使用uwsgi发布项目
1.先下载 uwsgi 指定豆瓣源下载 pip install -i https://pypi.douban.com/simple uwsgi 2.查看你的uwsgi基于那个python解释器运行的 ...
- [翻译] DDExpandableButton
DDExpandableButton https://github.com/ddebin/DDExpandableButton Purpose - 目的 DDExpandableButton is a ...
- 将JSON字典转换为Model文件
将JSON字典转换为Model文件 1. 一切尽在不言中 2. 源码 https://github.com/YouXianMing/CreateModelFromJson 3. 说明 如果你还在手动写 ...
- Office 365实现单点登录系列(5)—配置单点登录
这是单点登录系列的最后一篇文章,前面4篇文章其实都是在为这篇文章的内容做准备,我把这四篇文章的链接放在下面,如果大家有需要,可以参考我以下的链接: Office 365实现单点登录系列(1)—域环境搭 ...
- 数据挖掘比赛优秀经验贴-收集ing
(1)TOP5%Kaggler:如何在 Kaggle 首战中进入前 10% | 干货https://www.leiphone.com/news/201703/kCMQyffeP0qUgD9a.html ...
- (转)em重建全过程
该问题遇到N次,被郁闷N次,特此记录以备不时之需 由于n久不用em,而本机在公司使用dhcp自动获取ip,导致ip变化,而使em启动报出ora-12514 DBD ERROR: OCIServerAt ...
- ubuntu16.04安装visual-studio-code
微软主页的安装说明,https://code.visualstudio.com/docs/setup/linux 有一点英语基础就能看懂,写的很好,一切以官方文档为主 方法一:可以使用umake ...
- [SHOI2012]回家的路
题目背景 SHOI2012 D2T1 题目描述 2046 年 OI 城的城市轨道交通建设终于全部竣工,由于前期规划周密,建成后的轨道交通网络由2n2n条地铁线路构成,组成了一个nn纵nn横的交通网.如 ...