[leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/
题意:判断输入的字符串是否是合法的数。
解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅。本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢!
首先这个题有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:
代码:
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 '': 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
[leetcode]Valid Number @ Python的更多相关文章
- Valid Number @python
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 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 Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
- 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 Sudoku python
#数独(すうどく,Sūdoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.#数独盘 ...
- leetcode Single Number python
#question : Given an array of integers, every element appears twice except for one. Find that single ...
随机推荐
- 虚拟机zookeeper和hbase集群搭建
集群zookeeper dataDir=/usr/local/zookeeper/dataDir dataLogDir=/usr/local/zookeeper/dataLogDir # the po ...
- JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存
1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...
- CSS3组件化之ios版菊花loading
<div class="juhua-loading"> <div class="jh-circle1 jh-circle-ios">&l ...
- 机器学习之路: python 支持向量机 LinearSVC 手写字体识别
使用python3 学习sklearn中支持向量机api的使用 可以来到我的git下载源代码:https://github.com/linyi0604/MachineLearning # 导入手写字体 ...
- 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测
使用python3 学习了线性回归的api 分别使用逻辑斯蒂回归 和 随机参数估计回归 对良恶性肿瘤进行预测 我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://gith ...
- 大型系统中使用JMS优化技巧–Sun OpenMQ
我们先来看看在Sun OpenMQ系统中 一个持久.可靠的方式传送消息的步骤是怎么样的,如图所示: 查看大图请点击这里 在传送过程中,系统处理JMS消息分为以下两类: ■ 有效负荷消息,由生成方发 ...
- URAL 1876 Centipede's Morning
1876. Centipede's Morning Time limit: 0.5 secondMemory limit: 64 MB A centipede has 40 left feet and ...
- BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1475 Solved: 932[Submit][ ...
- opencv第一课 打开一个图片
#include<stdio.h>#include<stdlib.h>#include<opencv2\opencv.hpp>#include<iostrea ...
- Java_JSP自定义标签的开发与应用
在JSTL提供了四个标签库(核心标签库.国际化标签库.数据库标签库和XML标签库),涉及到了几十个标签.虽然这些标签可以完成比较复杂的工作,但它们仍然无法满足程序中的特殊需求.因此,就需要用户根据自己 ...