Valid Number @python
Validate if a given string is numeric.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
直接上代码,特别是解法二,引入 re 模块匹配,很精简强大!
class InputType:
INVALID = 0
SPACE = 1
SIGN =2
DIGIT =3
DOT = 4
EXPONENT =5 class Solution:
# @param s, a string
# @return a boolean
def isNumber(self, s):
transition_table = [[-1, 0, 3, 1, 2, -1], # next states for state 0
[-1, 8, -1, 1, 4, 5], # next states for state 1
[-1, -1, -1, 4, -1, -1], # next states for state 2
[-1, -1, -1, 1, 2, -1], # next states for state 3
[-1, 8, -1, 4, -1, 5], # next states for state 4
[-1, -1, 6, 7, -1, -1], # next states for state 5
[-1, -1, -1, 7, -1, -1], # next states for state 6
[-1, 8, -1, 7, -1, -1], # next states for state 7
[-1, 8, -1, -1, -1, -1]] # next states for state 8 state = 0
for char in s:
inputType = InputType.INVALID
if char.isspace():
inputType = InputType.SPACE;
elif char == '+' or char == '-':
inputType = InputType.SIGN
elif char in '':
inputType =InputType.DIGIT
elif char == '.':
inputType = InputType.DOT
elif char == 'e' or char =='E':
inputType = InputType.EXPONENT state = transition_table[state][inputType]
if state == -1:
return False
return state==1 or state==4 or state ==7 or state==8 def isNumber2(self,s):
import re
return bool(re.match("^\s*[\+\-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?\d+)?\s*$",s))
Valid Number @python的更多相关文章
- [leetcode]Valid Number @ Python
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
- 【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 & ...
- 配置域名服务器报错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 " ...
随机推荐
- Unity3d NGUI 地图
1,制作地图 方式1: NGUI,新建一个Atlas,为M,按照一定的比例如 1:400,绘制地图. 新建一个GameObject,为A,关联UIPanel.A的transform.localScal ...
- c#强制执行内存回收
[DllImport("psapi.dll")] private static extern int EmptyWorkingSet(int hProcess); GC.Colle ...
- linux文件锁
http://blog.chinaunix.net/uid-25324849-id-3077304.html 在SHELL中实现文件锁,有两种简单的方式.(1)一是利用普通文件,在脚本启动时检查特定文 ...
- 转载:scikit-learn学习之SVM算法
转载,http://blog.csdn.net/gamer_gyt 目录(?)[+] ========================================================= ...
- Redis持久化机制和恢复机制
Redis持久化方式有两种: (1)RDB 对内存中数据库状态进行快照 (2)AOF 把每条写命令都写入文件,类似mysql的binlog日志 RDB 将Redis在内存中的数据库状态保存到磁盘里面, ...
- 远程访问linux环境安装图形界面问题解决汇总
本文内容转摘于其他网页,仅用于学习: 通常Linux出现 DISPLAY 尚未设置 解决方法,在root用户目录下执行#xhost +: [root@TEST144239 ~]# xhost + ac ...
- [oracle] ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
ORACLE 32位数据库正常安装,sqlplus 正常连接数据库但是PL/SQL developer 64位却报出这个错误. 第一反应是缺少32位客户端.下载安装,配置完成后如图所示: 还是报这个错 ...
- js 删除DropDownList的选项
function del_DropDownList_Option() { var ddlXZ= document.getElementById("name&quo ...
- Silverlight开源框架SL提供便捷的二次开发银光框架
Silverlight开发框架SilverFrame欢迎咨询 基于Silverlight4.0开发,兼容Silverlight 5.0,SQLServer2005数据库.WCF: 本框架有清爽的前端界 ...
- linux下查看进程运行的时间
原文链接:http://www.centoscn.com/CentOS/2014/0403/2724.html 可通过ps 来查看,通过参数 -o 来查看 例: ps -eo pid,tty,user ...